I have a BLE peripheral running on Raspberry Pi and I'm trying to communicate with it via Android.
The goal is to read a characteristic, which is supposed to trigger a process on the peripheral side.
My code is as follows:
public class MainActivity extends AppCompatActivity
{
static final String PERIPHERAL_NAME = "Gobbledego";
static final String WIFI_SERVICE_UUID = "B87CF566-7A73-4347-B711-BB20F8A23D08";
static final String WIFI_SCAN_CHARACTERISTIC_UUID = "891FDFB1-3E77-4597-8483-4AB24ADC18CB";
private BluetoothLeScanner scanner;
private BluetoothGatt bluetoothGatt;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
scanner.startScan(scanCallback);
}
private ScanCallback scanCallback = new ScanCallback()
{
@Override
public void onScanResult(int callbackType, ScanResult result)
{
super.onScanResult(callbackType, result);
String name = result.getDevice().getName();
if (name != null && name.equals(PERIPHERAL_NAME))
{
scanner.stopScan(scanCallback);
bluetoothGatt = result.getDevice().connectGatt(MainActivity.this, false, gattCallback);
}
}
};
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
if (newState == BluetoothProfile.STATE_CONNECTED)
{
bluetoothGatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
if (status == BluetoothGatt.GATT_SUCCESS)
{
BluetoothGattService service = gatt.getService(UUID.fromString(WIFI_SERVICE_UUID));
BluetoothGattCharacteristic wifiScanCharacteristic = service.getCharacteristic(UUID.fromString(WIFI_SCAN_CHARACTERISTIC_UUID));
bluetoothGatt.readCharacteristic(wifiScanCharacteristic); // returns true
}
}
};
The scanning part and the connection part are working. The problem is that the read is never acknowledged on the peripheral side. I know that the peripheral side is OK because I have an iOS based solution which works as intended.
I wonder what could be the problem?
EDIT: Tested on a Nexus 5 with Android 6.0.1