public classBluetoothLeService extendsService {
privateBluetoothManager mBluetoothManager;privateBluetoothAdapter mBluetoothAdapter;privateString mBluetoothDeviceAddress;privateBluetoothGatt mBluetoothGatt;private intmConnectionState= STATE_DISCONNECTED;privateHandler mHandler= newHandler();private final static intSTATE_DISCONNECTED= 0;private final static intSTATE_CONNECTING= 1;private final static intSTATE_CONNECTED= 2;private final static intRUN_PERIOD= 3000;private final static intCOUNT_PERIOD= 3000;public final staticString ACTION_GATT_CONNECTED= "com.example.chongyanghu.ble.ACTION_GATT_CONNECTED";public final staticString ACTION_GATT_DISCONNECTED= "com.example.chongyanghu.ble.ACTION_GATT_DISCONNECTED";public final staticString ACTION_GATT_SERVICES_DISCOVER= "com.example.chongyanghu.ble.ACTION_GATT_SERVICES_DISCOVER";public final staticString EXTRA_DATA= "com.example.chongyanghu.ble.EXTRA_DATA";public final staticString ACTION_DATA_BATTERY= "com.example.chongyanghu.ble.ACTION_DATA_BATTERY";public final staticString ACTION_DATA_MENUFACTURER= "com.example.chongyanghu.ble.ACTION_DATA_MENUFACTURER";public final staticString ACTION_DATA_COUNT= "com.example.chongyanghu.ble.ACTION_DATA_COUNT";public final staticString ACTION_DATA_BLOODPRESSURE= "com.example.chongyanghu.ble.ACTION_DATA_BLOODPRESSURE";public final staticString ACTION_DATA_BODYWEIGHT= "com.example.chongyanghu.ble.ACTION_DATA_BODYWEIGHT";//public final static UUID UUID_HEART_RATE_MEASUREMENT = UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);public final staticString BATTERY_SERVICE= "0000180F-0000-1000-8000-00805f9b34fb";public final staticString BATTERY_CHARATERISTIC= "00002a19-0000-1000-8000-00805f9b34fb";public final staticString MANUFACTURER_SERVICE= "0000180a-0000-1000-8000-00805f9b34fb";public final staticString MANUFACTURER_CHARATERISTIC= "00002a29-0000-1000-8000-00805f9b34fb";public final staticString CUSTOM_SERVICE= "0000ffe0-0000-1000-8000-00805f9b34fb";public final staticString CUSTOM_CHARACTERISTIC= "0000ffe1-0000-1000-8000-00805f9b34fb";private finalIBinder mBinder= newLocalBind();@Nullable@OverridepublicIBinder onBind(Intent intent) {
returnmBinder;}
public classLocalBind extendsBinder {
BluetoothLeService getService() {
returnBluetoothLeService.this;}
}
@Overridepublic booleanonUnbind(Intent intent) {
close();return super.onUnbind(intent);}
public booleaninitialize() {
if(mBluetoothManager== null) {
mBluetoothManager= (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);if(mBluetoothManager== null) {
return false;}
}
mBluetoothAdapter= mBluetoothManager.getAdapter();if(mBluetoothAdapter== null) {
return false;}
return true;}
public voiddisconnect() {
if(mBluetoothAdapter== null|| mBluetoothGatt== null) {
return;}
mBluetoothGatt.disconnect();}
public booleanconnect(finalString address) {
if(mBluetoothAdapter== null|| address == null) {
return false;}
if(mBluetoothDeviceAddress!= null&& mBluetoothGatt!= null&& address.equals(mBluetoothDeviceAddress)) {
if(mBluetoothGatt.connect()) {
mConnectionState= STATE_CONNECTING;return true;} else{
return false;}
}
finalBluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);if(device == null) {
return false;}
mBluetoothGatt= device.connectGatt(this, false,mGattCallBack);mBluetoothDeviceAddress= address;mConnectionState= STATE_CONNECTING;return true;}
private finalBluetoothGattCallback mGattCallBack= newBluetoothGattCallback() {
@Overridepublic voidonConnectionStateChange(BluetoothGatt gatt, intstatus, intnewState) {
String intentAction;if(newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;mConnectionState= STATE_CONNECTED;broadcastUpdate(intentAction);mBluetoothGatt.discoverServices();} else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;mConnectionState= STATE_DISCONNECTED;broadcastUpdate(intentAction);}
}
@Overridepublic voidonCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
Log.d("demo","onCharacteristicChanged");byte[] data = characteristic.getValue();finalStringBuilder stringBuilder = newStringBuilder(data.length);for(bytebyteChar : data)
stringBuilder.append(String.format("%02X",byteChar));String builder = stringBuilder.toString();Log.d("demo","0x -->"+ builder);if(data[0] == (byte)0xC1){
broadcastUpdate(ACTION_DATA_COUNT,characteristic);}else if(data[0] == (byte)0xB1){
broadcastUpdate(ACTION_DATA_BLOODPRESSURE,characteristic);}else if(data[0] == (byte)0xB7){
broadcastUpdate(ACTION_DATA_BODYWEIGHT,characteristic);}
}
@Overridepublic voidonServicesDiscovered(BluetoothGatt gatt, intstatus) {
if(status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVER);}
}
@Overridepublic voidonDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, intstatus) {
UUID uuid = descriptor.getCharacteristic().getUuid();}
@Overridepublic voidonCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, intstatus) {
if(status == BluetoothGatt.GATT_SUCCESS) {
Log.d("demo","state = "+ String.valueOf(status == BluetoothGatt.GATT_SUCCESS));if(characteristic.getUuid().toString().equalsIgnoreCase(BATTERY_CHARATERISTIC)) {
Log.d("demo",String.valueOf(characteristic.getValue()[0]));broadcastUpdate(ACTION_DATA_BATTERY,characteristic);} else if(characteristic.getUuid().toString().equalsIgnoreCase(MANUFACTURER_CHARATERISTIC)) {
Log.d("demo","readManufacturer");broadcastUpdate(ACTION_DATA_MENUFACTURER,characteristic);}
}
}
@Overridepublic voidonCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, intstatus) {
super.onCharacteristicWrite(gatt,characteristic,status);}
};//打开设备的通知功能public voidsetCharacteristicNotification(BluetoothGattCharacteristic characteristic, booleanenable) {
Log.d("demo","setCharacteristicNotification");if(mBluetoothAdapter== null|| mBluetoothGatt== null) {
return;}
mBluetoothGatt.setCharacteristicNotification(characteristic,enable);BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);mBluetoothGatt.writeDescriptor(descriptor);}
public voidreadCharacteristic(BluetoothGattCharacteristic characteristic) {
if(mBluetoothGatt== null|| mBluetoothAdapter== null) {
return;}
Log.d("demo","readCharacteristic");mBluetoothGatt.readCharacteristic(characteristic);}
private voidbroadcastUpdate(String action) {
finalIntent intent = newIntent(action);sendBroadcast(intent);}
private voidbroadcastUpdate(String action,BluetoothGattCharacteristic characteristic) {
finalIntent intent = newIntent(action);String uuid = characteristic.getUuid().toString();Log.d("demo","into broadcastUpdate");if(uuid.equalsIgnoreCase(BATTERY_CHARATERISTIC)) {
byte[] databattery = characteristic.getValue();analysisData(intent,databattery);} else if(uuid.equalsIgnoreCase(MANUFACTURER_CHARATERISTIC)) {
final byte[] datamanufacturer = characteristic.getValue();intent.putExtra(EXTRA_DATA,newString(datamanufacturer));sendBroadcast(intent);} else if(uuid.equalsIgnoreCase(CUSTOM_CHARACTERISTIC)) {
byte[] data = characteristic.getValue();analysisData(intent,data);}
}
public voidanalysisData(Intent intent, byte[] data) {
if(data != null&& data.length> 0) {
finalStringBuilder stringBuilder = newStringBuilder(data.length);Log.d("demo",String.valueOf(data.length));for(bytebyteChar : data)
stringBuilder.append(String.format("%02X",byteChar));String builder = stringBuilder.toString();Log.d("demo","0x -->"+ builder);intent.putExtra(EXTRA_DATA,builder);sendBroadcast(intent);}
}
public voidclose() {
if(mBluetoothGatt== null) {
return;}
mBluetoothGatt.close();mBluetoothGatt= null;}
publicList getSupportedGattServices() {
if(mBluetoothGatt== null) return null;returnmBluetoothGatt.getServices();}
public voidreadBattery() {
Log.d("demo","readBattery");BluetoothGattService batteryService = mBluetoothGatt.getService(UUID.fromString(BATTERY_SERVICE));if(batteryService != null) {
mHandler.postDelayed(newRunnable() {
@Overridepublic voidrun() {
Handler mHandler = newHandler();BluetoothGattService manufacturerService = mBluetoothGatt.getService(UUID.fromString(MANUFACTURER_SERVICE));if(manufacturerService != null) {
mHandler.postDelayed(newRunnable() {
@Overridepublic voidrun() {
BluetoothGattService customService = mBluetoothGatt.getService(UUID.fromString(CUSTOM_SERVICE));if(customService != null) {
BluetoothGattCharacteristic customCharacteristic = customService.getCharacteristic(UUID.fromString(CUSTOM_CHARACTERISTIC));if(customCharacteristic != null) {
final intcharaProp = customCharacteristic.getProperties();if((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
setCharacteristicNotification(customCharacteristic, true);}
}
}
}
},COUNT_PERIOD);BluetoothGattCharacteristic manufacturerCharacteristic = manufacturerService.getCharacteristic(UUID.fromString(MANUFACTURER_CHARATERISTIC));Log.d("demo","manufacturerCharacteristic = "+ String.valueOf(manufacturerCharacteristic != null));final intcharaProp = manufacturerCharacteristic.getProperties();if((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
Log.d("demo","intoNotify--->"+ String.valueOf((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0));readCharacteristic(manufacturerCharacteristic);}
}
}
},RUN_PERIOD);BluetoothGattCharacteristic batteryCharacteristic = batteryService.getCharacteristic(UUID.fromString(BATTERY_CHARATERISTIC));if(batteryCharacteristic != null) {
readCharacteristic(batteryCharacteristic);}
}
}
}