I'm trying to connect a raspberry pi to my phone via Bluetooth LE. The raspberry pi scans for and connects to devices already, all I need to do it make my phone able to be connected to via the app I'm trying to develop for it. I have managed to make the phone advertise with the following code, but whenever I try to connect it always says "No data available". I can't seem to figure out how to get it to connect properly.
Here is my code:
BluetoothAdapter bluetoothAdapter;
String bluetoothAdapterString;
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
bluetoothAdapterString = String.valueOf(bluetoothAdapter);
BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
AdvertiseSettings settings = new AdvertiseSettings.Builder()
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)//*_LOW_POWER or BALANCED
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
.setConnectable(true)
.build();
ParcelUuid pUuid = new ParcelUuid(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));//this is a service UUID (according to https://stackoverflow.com/questions/11105891/how-do-i-find-the-proper-uuid)
//this UUID might need to be changed
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(true)
.addServiceUuid(pUuid)
.addServiceData(pUuid, "pleaseWork!".getBytes(StandardCharsets.UTF_8))//"UTF-8"
.build();
AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
@Override
public void onStartSuccess(AdvertiseSettings settingsInEffect) {
mainActivity.showMessage.show("bluetooth should be working");
super.onStartSuccess(settingsInEffect);
}
@Override
public void onStartFailure(int errorCode) {
mainActivity.showMessage.show("advertising failure....");
Log.e("BLE", "Advertising onStartFailure: " + errorCode);
super.onStartFailure(errorCode);
}
};
advertiser.startAdvertising(settings, data, advertisingCallback);
also, once the phone is connected to the raspberry pi, is there an easy way to transmit data between them?
I don't care about my current code, all I want in the end is for my phone to be discoverable and to be able to connect to the raspberry pi.
edit: clarity