Skip to content

Commit 3278fe3

Browse files
committed
Impreoved Sample: Added Scan, Connect, Write and Read features.
1 parent 0161754 commit 3278fe3

File tree

14 files changed

+578
-99
lines changed

14 files changed

+578
-99
lines changed

BluetoothClassicLibrary/src/main/java/com/github/douglasjunior/bluetoothclassiclibrary/BluetoothClassicService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.content.Intent;
2323
import android.content.IntentFilter;
2424
import android.media.AudioManager;
25-
import android.os.Looper;
2625
import android.support.annotation.RequiresPermission;
2726
import android.util.Log;
2827

@@ -231,8 +230,7 @@ public ConnectThread(BluetoothDevice device) {
231230
mmDevice = device;
232231
BluetoothSocket tmp = null;
233232

234-
// Get a BluetoothSocket for a connection with the
235-
// given BluetoothDevice
233+
// Get a BluetoothSocket for a connection with the given BluetoothDevice
236234
try {
237235
tmp = device.createRfcommSocketToServiceRecord(mConfig.uuid);
238236
} catch (Exception e) {

Sample/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ android {
66

77
defaultConfig {
88

9-
applicationId "com.example.sample"
9+
applicationId "com.github.douglasjunior.bluetoothsample"
1010
minSdkVersion 18
11-
targetSdkVersion 25
11+
targetSdkVersion 22
1212
versionCode 1
1313
versionName "1.0"
1414
}
@@ -23,6 +23,7 @@ android {
2323
dependencies {
2424
compile fileTree(include: ['*.jar'], dir: 'libs')
2525
compile 'com.android.support:appcompat-v7:25.0.0'
26+
compile 'com.android.support:cardview-v7:25.0.0'
2627
compile 'com.android.support:design:25.0.0'
2728
compile project(':BluetoothLowEnergyLibrary')
2829
}
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sample">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.github.douglasjunior.bluetoothsample">
34

4-
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
5-
android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
6-
<activity android:name=".MainActivity" android:label="@string/app_name"
7-
android:theme="@style/AppTheme.NoActionBar">
5+
<uses-permission android:name="android.permission.BLUETOOTH" />
6+
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
7+
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
8+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
9+
10+
<uses-feature android:name="android.hardware.bluetooth" />
11+
<uses-feature android:name="android.hardware.bluetooth_le" />
12+
13+
<uses-feature android:name="android.hardware.location.gps" />
14+
15+
<application
16+
android:icon="@mipmap/ic_launcher"
17+
android:label="@string/app_name"
18+
android:theme="@style/AppTheme.NoActionBar">
19+
<activity
20+
android:name=".MainActivity"
21+
android:label="@string/app_name">
822
<intent-filter>
923
<action android:name="android.intent.action.MAIN" />
1024

1125
<category android:name="android.intent.category.LAUNCHER" />
1226
</intent-filter>
1327
</activity>
28+
29+
<activity
30+
android:name=".DeviceActivity"
31+
android:label="@string/activity_device"
32+
android:windowSoftInputMode="adjustResize"/>
33+
34+
1435
</application>
1536

1637
</manifest>

Sample/src/main/java/com/example/sample/MainActivity.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.github.douglasjunior.bluetoothsample;
2+
3+
import android.os.Bundle;
4+
import android.support.design.widget.FloatingActionButton;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.widget.Toolbar;
7+
import android.util.Log;
8+
import android.view.View;
9+
import android.widget.EditText;
10+
11+
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothService;
12+
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothStatus;
13+
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothWriter;
14+
15+
/**
16+
* Created by dougl on 10/04/2017.
17+
*/
18+
19+
public class DeviceActivity extends AppCompatActivity implements BluetoothService.OnBluetoothEventCallback, View.OnClickListener {
20+
21+
private static final String TAG = "DeviceActivity";
22+
23+
private FloatingActionButton mFab;
24+
private EditText mEdRead;
25+
private EditText mEdWrite;
26+
27+
private BluetoothService mService;
28+
private BluetoothWriter mWriter;
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
setContentView(R.layout.activity_device);
34+
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
35+
setSupportActionBar(toolbar);
36+
37+
mFab = (FloatingActionButton) findViewById(R.id.fab);
38+
mFab.setOnClickListener(this);
39+
40+
mEdRead = (EditText) findViewById(R.id.ed_read);
41+
mEdWrite = (EditText) findViewById(R.id.ed_write);
42+
43+
mService = BluetoothService.getDefaultInstance();
44+
mWriter = new BluetoothWriter(mService);
45+
}
46+
47+
@Override
48+
protected void onResume() {
49+
super.onResume();
50+
mService.setOnEventCallback(this);
51+
}
52+
53+
@Override
54+
public void onDataRead(byte[] buffer, int length) {
55+
Log.d(TAG, "onDataRead: " + new String(buffer, 0, length));
56+
mEdRead.append("< " + new String(buffer, 0, length) + "\n");
57+
}
58+
59+
@Override
60+
public void onStatusChange(BluetoothStatus status) {
61+
Log.d(TAG, "onStatusChange: " + status);
62+
}
63+
64+
@Override
65+
public void onDeviceName(String deviceName) {
66+
Log.d(TAG, "onDeviceName: " + deviceName);
67+
}
68+
69+
@Override
70+
public void onToast(String message) {
71+
Log.d(TAG, "onToast");
72+
}
73+
74+
@Override
75+
public void onDataWrite(byte[] buffer) {
76+
Log.d(TAG, "onDataWrite");
77+
mEdRead.append("> " + new String(buffer));
78+
}
79+
80+
@Override
81+
public void onClick(View v) {
82+
mWriter.writeln(mEdWrite.getText().toString());
83+
mEdWrite.setText("");
84+
}
85+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.github.douglasjunior.bluetoothsample;
2+
3+
import android.bluetooth.BluetoothDevice;
4+
import android.content.Context;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.text.TextUtils;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.TextView;
11+
12+
import com.github.douglasjunior.bluetoothclassiclibrary.BluetoothDeviceDecorator;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collection;
16+
import java.util.List;
17+
import java.util.Set;
18+
19+
/**
20+
* Created by dougl on 10/04/2017.
21+
*/
22+
23+
public class DeviceItemAdapter extends RecyclerView.Adapter<DeviceItemAdapter.ViewHolder> {
24+
25+
private final Context mContext;
26+
private final List<BluetoothDeviceDecorator> mDevices;
27+
private final LayoutInflater mInflater;
28+
private OnAdapterItemClickListener mOnItemClickListener;
29+
30+
31+
public DeviceItemAdapter(Context context, List<BluetoothDevice> devices) {
32+
super();
33+
mContext = context;
34+
mDevices = decorateDevices(devices);
35+
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
36+
}
37+
38+
public static List<BluetoothDeviceDecorator> decorateDevices(Collection<BluetoothDevice> btDevices) {
39+
List<BluetoothDeviceDecorator> devices = new ArrayList<>();
40+
for (BluetoothDevice dev : btDevices) {
41+
devices.add(new BluetoothDeviceDecorator(dev, 0));
42+
}
43+
return devices;
44+
}
45+
46+
public DeviceItemAdapter(Context context, Set<BluetoothDevice> devices) {
47+
this(context, new ArrayList<>(devices));
48+
}
49+
50+
@Override
51+
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
52+
ViewHolder vh = new ViewHolder(mInflater.inflate(R.layout.device_item, parent, false));
53+
return vh;
54+
}
55+
56+
@Override
57+
public void onBindViewHolder(ViewHolder holder, final int position) {
58+
final BluetoothDeviceDecorator device = mDevices.get(position);
59+
60+
holder.tvName.setText(TextUtils.isEmpty(device.getName()) ? "---" : device.getName());
61+
holder.tvAddress.setText(device.getAddress());
62+
holder.tvRSSI.setText(device.getRSSI() + "");
63+
64+
holder.itemView.setOnClickListener(new View.OnClickListener() {
65+
@Override
66+
public void onClick(View v) {
67+
mOnItemClickListener.onItemClick(device, position);
68+
}
69+
});
70+
}
71+
72+
@Override
73+
public int getItemCount() {
74+
return mDevices.size();
75+
}
76+
77+
public List<BluetoothDeviceDecorator> getDevices() {
78+
return mDevices;
79+
}
80+
81+
public void setOnAdapterItemClickListener(OnAdapterItemClickListener onItemClickListener) {
82+
mOnItemClickListener = onItemClickListener;
83+
}
84+
85+
public interface OnAdapterItemClickListener {
86+
public void onItemClick(BluetoothDeviceDecorator device, int position);
87+
}
88+
89+
public static class ViewHolder extends RecyclerView.ViewHolder {
90+
91+
private final TextView tvName;
92+
private final TextView tvAddress;
93+
private final TextView tvRSSI;
94+
95+
public ViewHolder(View itemView) {
96+
super(itemView);
97+
tvName = (TextView) itemView.findViewById(R.id.tv_name);
98+
tvAddress = (TextView) itemView.findViewById(R.id.tv_address);
99+
tvRSSI = (TextView) itemView.findViewById(R.id.tv_rssi);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)