[英]BLE(Bluetooth Low Energy)
I try to implement Connection between two devices(Any ios/Android) and transfer text using BLE
I try too many demos from GitHub and also try from documentation step by step but I am not getting a success
some time give Error like :
> GATT server 133
or
>android.bluetooth.BluetoothGattCharacteristic android.bluetooth.BluetoothGattService.getCharacteristic(java.util.UUID)' on a null object reference
Please help me
Thank you so much
Here is the my two activity code one ble class and one activity to call function from BLE claass
I want just pass small text to android to android or android to ios using ble
please help I tried from too many days
================================================================
public class MainActivity extends AppCompatActivity {
ArrayList<String> getDevicess;
List<BluetoothDevice> getConnectedDevicess;
TextView tvscan;
TextView tvConnected;
TextView tvsend;
Ble ble;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvsend = (TextView) findViewById(R.id.tvsend);
getDevicess = new ArrayList<>();
getConnectedDevicess = new ArrayList<>();
ble = new Ble(this, getApplicationContext(), "zad");
ble.enableBle();
ble.checkPermission(0);
ble.scanLeDevice(true, 1000);
//after scan and connect click on send button then give error
tvsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ble.writeCharacteristics(hexStringToByteArray("abcdefg"), UUID.fromString("fffffff0-00f7-4000-b000-000000000000"), UUID.fromString("fffffff5-00f7-4000-b000-000000000000"));
}
});
}
public byte[] hexStringToByteArray(String s) {
int len = s.length()-1;
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}
===========================================BLE CLass==================
// Function To Write Characteristics .. ! // 写特征的函数.. ! @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public void writeCharacteristics(byte[] data, UUID DLE_SERVICE, UUID DLE_WRITE_CHAR) { BluetoothGattService Service = mBluetoothGatt.getService(DLE_SERVICE); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public void writeCharacteristics(byte[] data, UUID DLE_SERVICE, UUID DLE_WRITE_CHAR) { BluetoothGattService Service = mBluetoothGatt.getService(DLE_SERVICE); if (Service == null) { Log.d(TAG, "service not found!"); if (Service == null) { Log.d(TAG, "service not found!"); //return false; //返回假; } }
BluetoothGattCharacteristic charc1 = Service.getCharacteristic(DLE_WRITE_CHAR);
if (charc1 == null) {
Log.d(TAG, "char not found!");
Log.d(TAG, "CHARAC_-TRST" + charc1.getUuid());
// return false;
}
// charc1.setValue(new byte[]{0x00, 0x05, 0x10, 0x01, 0x3E, 0x01, 0x23});
charc1.setValue(data);
boolean stat = mBluetoothGatt.writeCharacteristic(charc1);
Log.d(TAG, "FINISHED WRITTING CHAR 1 status write :(status)" + stat);
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for (byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
}
}
This snippet is problematic:这个片段有问题:
public void displayBleService(List<BluetoothGattService> gattServices) {
Log.d(TAG, "display Services");
List<BluetoothGattCharacteristic> characteristics = new ArrayList<BluetoothGattCharacteristic>();
for (BluetoothGattService services : gattServices) {
Log.d(TAG, "SERVICES == ." + services.getUuid());
characteristics = services.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
Log.d(TAG, "CAHRACTERISTICS == ." + characteristic.getUuid() + "--" + characteristic.getProperties());
//enableNotification(characteristic, true);
}
}
}
the services
is null, and thus the services.getCharacteristics()
is provoking the NPE. services
为空,因此services.getCharacteristics()
引发了 NPE。
Please debug your code, and make sure you have BLUETOOTH_ADMIN
permission as well.请调试您的代码,并确保您也拥有BLUETOOTH_ADMIN
权限。 Feel free to add necessary null-guards as well at your own discretion.您也可以自行决定添加必要的空值保护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.