Can';不要使用ANDROID BLE api编写任何特性
在我的应用程序中,我想为一个可编程设备(iBeacon)编写一些特性。问题是,在我调用Can';不要使用ANDROID BLE api编写任何特性,android,bluetooth-lowenergy,ibeacon,android-bluetooth,ibeacon-android,Android,Bluetooth Lowenergy,Ibeacon,Android Bluetooth,Ibeacon Android,在我的应用程序中,我想为一个可编程设备(iBeacon)编写一些特性。问题是,在我调用gatt.executeLabelWrite()之后,信标将与我的智能手机断开连接,并调用onConnectionStateChange回调-onReliableWriteCompleted方法。当然,这个特征并没有写在信标上。所以我的问题是-我做错了什么?我如何使用executeleablerwrite()methodCallback方法将特征写入可编程设备: @Override
gatt.executeLabelWrite()
之后,信标将与我的智能手机断开连接,并调用onConnectionStateChange
回调-onReliableWriteCompleted
方法。当然,这个特征并没有写在信标上。所以我的问题是-我做错了什么?我如何使用executeleablerwrite()
methodCallback方法将特征写入可编程设备:
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
mBluetoothGatt.discoverServices();
mAlreadyConnected = true;
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
if (mWritingStarted) {
reconnectBeacon();
}
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
writeNextCharacteristic(mCurrentCharacteristic);
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (characteristic.getValue() != mCurrentValue) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
gatt.abortReliableWrite();
} else {
gatt.abortReliableWrite(mBluetoothDevice);
}
} else {
boolean result = gatt.executeReliableWrite();
}
}
@Override
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
super.onReliableWriteCompleted(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
mCurrentCharacteristic++;
writeNextCharacteristic(mCurrentCharacteristic);
} else {
//TODO: handle error
}
}
我用于编写特征的其他方法:
private void connectBeacon() {
mBluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = mBluetoothManager.getAdapter();
mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(mMacAddress);
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
}
private void reconnectBeacon() {
mBluetoothGatt = mBluetoothDevice.connectGatt(mContext, false, mGattCallback);
}
private void writeNextCharacteristic(int characteristicPosition) {
if (characteristicPosition < mCharacteristicsToWrite.size()) {
UUID serviceUuid = mCharacteristicsToWrite.get(characteristicPosition).getServiceUuid();
UUID characteristicUuid = mCharacteristicsToWrite.get(characteristicPosition).getCharacteristicUuid();
byte[] value = mCharacteristicsToWrite.get(characteristicPosition).getValue();
mCurrentValue = value;
BluetoothGattCharacteristic gattCharacteristic = mBluetoothGatt.getService(serviceUuid).getCharacteristic(characteristicUuid);
mBluetoothGatt.beginReliableWrite();
gattCharacteristic.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(gattCharacteristic);
Log.d("Status: ", String.valueOf(status));
} else {
mWritingStarted = false;
mBluetoothGatt.close();
mCharacteristicWriteCallback.onAllCharacteristicsWritten();
}
}
private void connectBeacon(){
mBluetoothManager=(BluetoothManager)mContext.getSystemService(Context.BLUETOOTH_服务);
mBluetoothAdapter=mBluetoothManager.getAdapter();
mBluetoothDevice=mBluetoothAdapter.getRemoteDevice(mmacadAddress);
mBluetoothGatt=mBluetoothDevice.connectGatt(mContext,false,mGattCallback);
}
私有无效重新连接信标(){
mBluetoothGatt=mBluetoothDevice.connectGatt(mContext,false,mGattCallback);
}
私有void writeNextCharacteristic(int-characteristicPosition){
if(特征位置
信标是否可能正在关闭连接?也许Android代码根本没有问题。@DavidYoung我做了一些测试,这个问题与Android或我的特定设备有关。信标与我从Google Play下载并尝试使用的所有应用程序断开连接。同时,iOS应用程序工作正常。我能做些什么来修复它?当你说“iOS应用程序运行正常”是什么意思?您是否能够使用不同的iOS应用程序连接到信标并写入相同的GATT特征?编辑您的问题以添加此信息将很有帮助。@davidgyoung是的。我可以使用iOS应用程序编写相同的特性。我面临着类似的问题,您已经找到解决方案了吗?