I am currently trying to create an application which will enable Android and IOS devices to connect to each other via Bluetooth and then send messages to one another. Below is my code for this for the android application
MAINACTIVITY.JAVA
public class MainActivity extends AppCompatActivity {
private BluetoothAdapter bluetoothAdapter;
private BluetoothGatt gatt;
private BluetoothGattCharacteristic inputCharacteristic;
private TextView outputView;
private EditText inputView;
private static final int REQUEST_ENABLE_BT = 1;
BluetoothAdapter.LeScanCallback leScanCallback;
public void receiveMode(View v) {
bluetoothAdapter.startLeScan(leScanCallback);
}
public void sendMessage(View v) {
inputCharacteristic.setValue(inputView.getText().toString());
gatt.writeCharacteristic(inputCharacteristic);
}
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
final String value = characteristic.getStringValue(0);
runOnUiThread(new Runnable() {
@Override
public void run() {
outputView.setText(value);
}
});
}
}
@Override
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
MainActivity.this.gatt = gatt;
if (newState == BluetoothProfile.STATE_CONNECTED) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
List<BluetoothGattService> services = gatt.getServices();
for (BluetoothGattService service : services) {
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
if (getString(R.string.outputUUID).equals(characteristic.getUuid().toString())) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptors().get(0);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
} else if (getString(R.string.inputUUID).equals(characteristic.getUuid().toString())) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
inputCharacteristic = characteristic;
}
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputView = (TextView) findViewById(R.id.outputText);
inputView = (EditText) findViewById(R.id.inputText);
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
bluetoothAdapter = bluetoothManager.getAdapter();
if (bluetoothAdapter != null && !bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
I have not been able to get this application working as I am receiving a null callback from the android monitor when the startLeScan is run. I believe this is because this method has been deprecated and therfore doesn't work anymore. Can someone edit my code to make it use an alternative method to scan for devices?
This is the error message I recieved:
08-02 11:36:06.470 31188-31188/uk.ac.york.androidtoiosble D/BluetoothAdapter: startLeScan(): null 08-02 11:36:06.470 31188-31188/uk.ac.york.androidtoiosble E/BluetoothAdapter: startLeScan: null callback
For starters, you get a null callback because you literally never created one.
However, to answer your second question
use an alternative method to scan for devices?
Your IDE is probably telling you this, but reading the documentation, you would have seen....
This method was deprecated in API level 21. use
startScan(List, ScanSettings, ScanCallback)
instead.
But again, you must explicitly create your ScanCallback
object.
Read over this to get an idea of how you can find Bluetooth devices
https://developer.android.com/guide/topics/connectivity/bluetooth.html#FindingDevices
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.