Отправка данных на устройство BLE не работает. BLE Устройство подключено успешно, после успешного подключения затем записывается шестнадцатеричная строка ("1b6745324f998ac5720200303345678912c345677bgdfb c"). Если представленная шестнадцатеричная строка действительна, то мигает красный свет, я пробовал выше шестнадцатеричной строки, вставленной в приложение LightBlue, она мигает красным светом, а другие строки недопустимы. Я пытаюсь работать над тем же требованием.
пожалуйста, проверьте мой ниже код .
import static android.bluetooth.BluetoothAdapter.STATE_DISCONNECTED;
public class BLEManager extends Activity {
BluetoothAdapter bTAdapter;
BluetoothGatt bluetoothGatt;
BluetoothLeScanner scanner;
private String mServiceUUID = "b456543453211-23ac-5a23-1313-b12312123342";
private Handler handler = new Handler();
private LinearLayout stopUserInteraction;
/***
* Callback for monitoring the status of ble device
*/
BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothGatt.STATE_CONNECTED) {
bluetoothGatt.discoverServices();
}else if (newState == BluetoothGatt.STATE_DISCONNECTED) {
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
readCharacteristic();
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.e("Testing", "Write Status " +status);
gatt.readCharacteristic(characteristic);
Log.e("Testing", "Write Status Test" + characteristic.getValue());
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
}
};
private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Map<Integer, String> deviceData = ParseRecord(scanRecord);
if (deviceData.get(-1) != null && processData(deviceData.get(-1))) {
stopScanning();
Log.e("Test", "Device Found" + device.getAddress());
bluetoothGatt = device.connectGatt(BLEManager.this, false,
bluetoothGattCallback);
bTAdapter.stopLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
}
});
}
}
};
public void stopScanning() {
AsyncTask.execute(new Runnable() {
@Override
public void run() {
scanner = bTAdapter.getBluetoothLeScanner();
bTAdapter.stopLeScan(leScanCallback);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ble_manager);
bTAdapter = BluetoothAdapter.getDefaultAdapter();
scanner = bTAdapter.getBluetoothLeScanner();
bTAdapter.startLeScan(leScanCallback);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
if (bTAdapter != null && bTAdapter.isEnabled()) {
bTAdapter.stopLeScan(new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
}
});
}
}
/***
* Get services and characteristic data
*/
private void readCharacteristic() {
BluetoothGattService mCustomService =
bluetoothGatt.getService(UUID.fromString(mServiceUUID));
if (mCustomService != null) {
final List<BluetoothGattCharacteristic> characteristics = mCustomService.getCharacteristics();
for (final BluetoothGattCharacteristic bluetoothGattCharacteristic : characteristics) {
String dataToWrite = "1b6745324f998ac5720200303345678912c345677bgdfbc";
Log.e("UUID:",mServiceUUID);
bluetoothGattCharacteristic.setValue(dataToWrite);
Log.e("Testing", "bluetoothGattCharacteristic: " + bluetoothGattCharacteristic.getValue());
bluetoothGatt.writeCharacteristic(bluetoothGattCharacteristic);
}
} else {
Log.e("Testing", "Service null");
}
}
}