I am trying to connect to a BLE device using the MAC address.
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(rememberedDeviceAddress)
bluetoothDevice.
Certain devices require Bluetooth LE interactions to be run on the UI thread. So I would recommend trying something like this:
// Create handler for main thread where mContext is application context
mHandler = new Handler(mContext.getMainLooper());
...
// Connect to BLE device from mHandler
mHandler.post(new Runnable() {
@Override
public void run() {
mBTGatt = mBTDevice.connectGatt(mContext, false, mGattCallback);
}
});
Of course you could use Activity.runOnUiThread as well. Source: https://stackoverflow.com/a/23478737