|
|
|
@ -7,6 +7,7 @@ import android.bluetooth.BluetoothDevice;
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGatt;
|
|
|
|
import android.bluetooth.BluetoothGattCallback;
|
|
|
|
import android.bluetooth.BluetoothGattCallback;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
|
import android.bluetooth.BluetoothGattCharacteristic;
|
|
|
|
|
|
|
|
import android.bluetooth.BluetoothGattDescriptor;
|
|
|
|
import android.bluetooth.BluetoothGattService;
|
|
|
|
import android.bluetooth.BluetoothGattService;
|
|
|
|
import android.bluetooth.BluetoothManager;
|
|
|
|
import android.bluetooth.BluetoothManager;
|
|
|
|
import android.bluetooth.BluetoothProfile;
|
|
|
|
import android.bluetooth.BluetoothProfile;
|
|
|
|
@ -14,19 +15,22 @@ import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.os.Binder;
|
|
|
|
import android.os.Binder;
|
|
|
|
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BluetoothLeService extends Service {
|
|
|
|
public class BluetoothLeService extends Service {
|
|
|
|
|
|
|
|
|
|
|
|
private final static String TAG = BluetoothLeService.class.getSimpleName();
|
|
|
|
private final static String TAG = "app";
|
|
|
|
|
|
|
|
|
|
|
|
private BluetoothManager mBluetoothManager;
|
|
|
|
private BluetoothManager mBluetoothManager;
|
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
|
private BluetoothAdapter mBluetoothAdapter;
|
|
|
|
@ -49,6 +53,14 @@ public class BluetoothLeService extends Service {
|
|
|
|
public final static String EXTRA_DATA =
|
|
|
|
public final static String EXTRA_DATA =
|
|
|
|
"com.example.bluetooth.le.EXTRA_DATA";
|
|
|
|
"com.example.bluetooth.le.EXTRA_DATA";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public BluetoothGattCharacteristic characteristicWrite = null;
|
|
|
|
|
|
|
|
public BluetoothGattCharacteristic characteristicRead = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 替换为你的服务和特征UUID
|
|
|
|
|
|
|
|
private static final UUID SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");//服务uuid
|
|
|
|
|
|
|
|
private static final UUID CHARACTERISTIC_UUID_READ = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");//读取蓝牙uuid
|
|
|
|
|
|
|
|
private static final UUID CHARACTERISTIC_UUID_WRITE = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");//蓝牙写入数据uuid
|
|
|
|
|
|
|
|
private static final UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");//开启蓝牙通知配置,固定写死
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Implements callback methods for GATT events that the app cares about. For example,
|
|
|
|
// Implements callback methods for GATT events that the app cares about. For example,
|
|
|
|
@ -58,25 +70,20 @@ public class BluetoothLeService extends Service {
|
|
|
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
|
|
|
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
|
|
|
|
String intentAction;
|
|
|
|
String intentAction;
|
|
|
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
|
|
|
if (newState == BluetoothProfile.STATE_CONNECTED) {
|
|
|
|
|
|
|
|
// 连接成功,开始发现服务
|
|
|
|
intentAction = ACTION_GATT_CONNECTED;
|
|
|
|
intentAction = ACTION_GATT_CONNECTED;
|
|
|
|
mConnectionState = STATE_CONNECTED;
|
|
|
|
mConnectionState = STATE_CONNECTED;
|
|
|
|
broadcastUpdate(intentAction);
|
|
|
|
broadcastUpdate(intentAction);
|
|
|
|
Log.i(TAG, "Connected to GATT server.");
|
|
|
|
Log.i(TAG, "Connected to GATT server.");
|
|
|
|
// Attempts to discover services after successful connection.
|
|
|
|
// Attempts to discover services after successful connection.
|
|
|
|
if (ActivityCompat.checkSelfPermission(BluetoothLeService.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(BluetoothLeService.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Log.i(TAG, "Attempting to start service discovery:" +
|
|
|
|
boolean isDiscovery = mBluetoothGatt.discoverServices();
|
|
|
|
mBluetoothGatt.discoverServices());
|
|
|
|
Log.i(TAG, "Attempting to start service discovery:" + isDiscovery);
|
|
|
|
|
|
|
|
|
|
|
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
|
|
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
|
|
|
|
|
|
|
mBluetoothGatt.close();
|
|
|
|
intentAction = ACTION_GATT_DISCONNECTED;
|
|
|
|
intentAction = ACTION_GATT_DISCONNECTED;
|
|
|
|
mConnectionState = STATE_DISCONNECTED;
|
|
|
|
mConnectionState = STATE_DISCONNECTED;
|
|
|
|
Log.i(TAG, "Disconnected from GATT server.");
|
|
|
|
Log.i(TAG, "Disconnected from GATT server.");
|
|
|
|
@ -86,34 +93,68 @@ public class BluetoothLeService extends Service {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
|
|
|
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
|
|
|
|
|
|
|
|
super.onServicesDiscovered(gatt, status);
|
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
|
|
|
|
BluetoothGattService service = gatt.getService(SERVICE_UUID);
|
|
|
|
////tsp默认选择一个地址
|
|
|
|
if (service != null) {
|
|
|
|
BluetoothGattCharacteristic characteristic1=
|
|
|
|
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID_READ);
|
|
|
|
gatt.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb")).getCharacteristic(UUID.fromString("0000ffe4-0000-1000-8000-00805f9b34fb"));
|
|
|
|
if (characteristic != null) {
|
|
|
|
setCharacteristicNotification(characteristic1,true);
|
|
|
|
// 启用通知
|
|
|
|
|
|
|
|
enableCharacteristicNotification(gatt, characteristic);
|
|
|
|
|
|
|
|
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "onServicesDiscovered received: " + status);
|
|
|
|
Log.i(TAG, "onServicesDiscovered received: " + status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
|
|
|
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
|
|
|
|
|
|
|
super.onCharacteristicRead(gatt, characteristic, status);
|
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
|
|
|
|
//普通读取蓝牙设备数据,有些设备会用到,当前蓝牙传感器没用这个
|
|
|
|
|
|
|
|
byte[] data = characteristic.getValue();
|
|
|
|
|
|
|
|
String receivedData = new String(data);
|
|
|
|
|
|
|
|
Log.i(TAG, "读取数据: " + receivedData);
|
|
|
|
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
|
|
|
|
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
|
|
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
|
|
|
|
|
|
super.onCharacteristicChanged(gatt, characteristic);
|
|
|
|
|
|
|
|
// 收到通知数据
|
|
|
|
|
|
|
|
byte[] data = characteristic.getValue();
|
|
|
|
|
|
|
|
String receivedData = new String(data);
|
|
|
|
|
|
|
|
Log.i(TAG, "收到通知数据: " + receivedData);
|
|
|
|
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
|
|
|
|
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
|
|
|
|
|
|
|
|
super.onCharacteristicWrite(gatt, characteristic, status);
|
|
|
|
|
|
|
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
|
|
|
|
|
|
|
Log.i(TAG, "Data written successfully");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.e(TAG, "Failed to write data: " + status);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void enableCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
gatt.setCharacteristicNotification(characteristic, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
|
|
|
|
|
|
|
|
if (descriptor != null) {
|
|
|
|
|
|
|
|
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
|
|
|
|
|
|
|
|
gatt.writeDescriptor(descriptor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void broadcastUpdate(final String action) {
|
|
|
|
private void broadcastUpdate(final String action) {
|
|
|
|
final Intent intent = new Intent(action);
|
|
|
|
final Intent intent = new Intent(action);
|
|
|
|
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
|
|
|
|
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
|
|
|
|
@ -123,13 +164,18 @@ public class BluetoothLeService extends Service {
|
|
|
|
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
|
|
|
|
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
|
|
|
|
final Intent intent = new Intent(action);
|
|
|
|
final Intent intent = new Intent(action);
|
|
|
|
// For all other profiles, writes the data formatted in HEX.
|
|
|
|
// For all other profiles, writes the data formatted in HEX.
|
|
|
|
final byte[] data = characteristic.getValue();
|
|
|
|
// final byte[] data = characteristic.getValue();
|
|
|
|
if (data != null && data.length > 0) {
|
|
|
|
// 收到通知数据
|
|
|
|
final StringBuilder stringBuilder = new StringBuilder(data.length);
|
|
|
|
byte[] data = characteristic.getValue();
|
|
|
|
for(byte byteChar : data)
|
|
|
|
String receivedData = new String(data, StandardCharsets.UTF_8);
|
|
|
|
stringBuilder.append(String.format("%02X ", byteChar));
|
|
|
|
//Log.i(TAG, "收到通知数据: " + receivedData);
|
|
|
|
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
|
|
|
|
// if (data != null && data.length > 0) {
|
|
|
|
}
|
|
|
|
// final StringBuilder stringBuilder = new StringBuilder(data.length);
|
|
|
|
|
|
|
|
// for (byte byteChar : data)
|
|
|
|
|
|
|
|
// stringBuilder.append(String.format("%02X ", byteChar));
|
|
|
|
|
|
|
|
// intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
intent.putExtra(EXTRA_DATA, receivedData);
|
|
|
|
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
|
|
|
|
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -184,11 +230,10 @@ public class BluetoothLeService extends Service {
|
|
|
|
* Connects to the GATT server hosted on the Bluetooth LE device.
|
|
|
|
* Connects to the GATT server hosted on the Bluetooth LE device.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param address The device address of the destination device.
|
|
|
|
* @param address The device address of the destination device.
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return Return true if the connection is initiated successfully. The connection result
|
|
|
|
* @return Return true if the connection is initiated successfully. The connection result
|
|
|
|
* is reported asynchronously through the
|
|
|
|
* is reported asynchronously through the
|
|
|
|
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
|
|
|
|
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
|
|
|
|
* callback.
|
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public boolean connect(final String address) {//连接设备
|
|
|
|
public boolean connect(final String address) {//连接设备
|
|
|
|
if (mBluetoothAdapter == null || address == null) {
|
|
|
|
if (mBluetoothAdapter == null || address == null) {
|
|
|
|
@ -197,16 +242,9 @@ public class BluetoothLeService extends Service {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Previously connected device. Try to reconnect.
|
|
|
|
// Previously connected device. Try to reconnect.
|
|
|
|
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {
|
|
|
|
if (address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
|
|
|
|
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
|
|
|
|
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mBluetoothGatt.connect()) {
|
|
|
|
if (mBluetoothGatt.connect()) {
|
|
|
|
@ -224,13 +262,36 @@ public class BluetoothLeService extends Service {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We want to directly connect to the device, so we are setting the autoConnect
|
|
|
|
// We want to directly connect to the device, so we are setting the autoConnect
|
|
|
|
// parameter to false.
|
|
|
|
// parameter to false.
|
|
|
|
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
|
|
|
|
//mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
|
|
|
|
|
|
|
|
mBluetoothGatt = device.connectGatt(this, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
|
|
|
|
Log.d(TAG, "Trying to create a new connection.");
|
|
|
|
Log.d(TAG, "Trying to create a new connection.");
|
|
|
|
mBluetoothDeviceAddress = address;
|
|
|
|
mBluetoothDeviceAddress = address;
|
|
|
|
mConnectionState = STATE_CONNECTING;
|
|
|
|
mConnectionState = STATE_CONNECTING;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean checkBleConnect(final String address) {
|
|
|
|
|
|
|
|
if (mBluetoothAdapter == null || address == null) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
|
|
|
|
|
|
|
|
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBluetoothGatt.connect()) {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
|
|
|
|
|
|
|
|
if (device == null) {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Disconnects an existing connection or cancel a pending connection. The disconnection result
|
|
|
|
* Disconnects an existing connection or cancel a pending connection. The disconnection result
|
|
|
|
* is reported asynchronously through the
|
|
|
|
* is reported asynchronously through the
|
|
|
|
@ -243,16 +304,11 @@ public class BluetoothLeService extends Service {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// TODO: Consider calling
|
|
|
|
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBluetoothGatt.disconnect();
|
|
|
|
mBluetoothGatt.disconnect();
|
|
|
|
|
|
|
|
mBluetoothGatt.close();
|
|
|
|
|
|
|
|
mBluetoothGatt = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
@ -277,70 +333,54 @@ public class BluetoothLeService extends Service {
|
|
|
|
mBluetoothGatt = null;
|
|
|
|
mBluetoothGatt = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
public boolean writeData(byte[] data) {
|
|
|
|
* Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
|
|
|
|
if (mBluetoothGatt == null) {
|
|
|
|
* asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
|
|
|
|
Log.e("TAG", "BluetoothGatt is null");
|
|
|
|
* callback.
|
|
|
|
return false;
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param characteristic The characteristic to read from.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
|
|
|
|
|
|
|
|
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
|
|
|
|
|
|
|
|
Log.w(TAG, "BluetoothAdapter not initialized");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
|
|
|
// TODO: Consider calling
|
|
|
|
BluetoothGattService service = mBluetoothGatt.getService(SERVICE_UUID);
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
if (service == null) {
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
Log.e("TAG", "Service not found");
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
return false;
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBluetoothGatt.readCharacteristic(characteristic);
|
|
|
|
|
|
|
|
}
|
|
|
|
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID_WRITE);
|
|
|
|
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
|
|
|
|
if (characteristic == null) {
|
|
|
|
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
|
|
|
|
Log.e("TAG", "Characteristic not found");
|
|
|
|
Log.w(TAG, "BluetoothAdapter not initialized");
|
|
|
|
return false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置写入类型
|
|
|
|
|
|
|
|
int properties = characteristic.getProperties();
|
|
|
|
|
|
|
|
if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
|
|
|
|
|
|
|
|
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
|
|
|
|
|
|
|
|
} else if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
|
|
|
|
|
|
|
|
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
characteristic.setValue(data);
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
// TODO: Consider calling
|
|
|
|
return false;
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBluetoothGatt.writeCharacteristic(characteristic);
|
|
|
|
return mBluetoothGatt.writeCharacteristic(characteristic);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Enables or disables notification on a give characteristic.
|
|
|
|
// 写入字符串数据
|
|
|
|
*
|
|
|
|
public boolean writeString(String message) {
|
|
|
|
* @param characteristic Characteristic to act on.
|
|
|
|
byte[] data = message.getBytes(StandardCharsets.UTF_8);
|
|
|
|
* @param enabled If true, enable notification. False otherwise.
|
|
|
|
return writeData(data);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
//选择地址
|
|
|
|
|
|
|
|
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
|
|
|
|
// 写入16进制数据
|
|
|
|
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
|
|
|
|
public boolean writeHex(String hexString) {
|
|
|
|
Log.w(TAG, "BluetoothAdapter not initialized");
|
|
|
|
int len = hexString.length();
|
|
|
|
return;
|
|
|
|
byte[] data = new byte[len / 2];
|
|
|
|
}
|
|
|
|
for (int i = 0; i < len; i += 2) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
|
|
|
|
// TODO: Consider calling
|
|
|
|
+ Character.digit(hexString.charAt(i + 1), 16));
|
|
|
|
// ActivityCompat#requestPermissions
|
|
|
|
|
|
|
|
// here to request the missing permissions, and then overriding
|
|
|
|
|
|
|
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
|
|
|
|
|
|
|
// int[] grantResults)
|
|
|
|
|
|
|
|
// to handle the case where the user grants the permission. See the documentation
|
|
|
|
|
|
|
|
// for ActivityCompat#requestPermissions for more details.
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
|
|
|
|
return writeData(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<BluetoothGattService> getSupportedGattServices() {
|
|
|
|
public List<BluetoothGattService> getSupportedGattServices() {
|
|
|
|
|