-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Prateek Rokadiya edited this page Feb 7, 2019
·
1 revision
SimpleBluetooth
Native Usage of Bluetooth API's from Android Documentation for Bluetooth
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
private BluetoothAdapter mBluetoothAdapter;
if (mBluetoothAdapter == null) {
// Device doesn't support Bluetooth
}
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); //REQUEST_ENABLE_BT = 1
mBluetoothAdapter.enable();
mBluetoothAdapter.disable();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter); //BroadcastReceiver mReceiver
Call permission programatically as well (otherwise - Scanning results won't appear, A must TODO since Android.ver > 6.0)
int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
if (state == BluetoothAdapter.STATE_ON) {
Log.d(TAG, "onReceive: Bluetooth is ON");
}else if(state == BluetoothAdapter.STATE_OFF){
Log.d(TAG, "onReceive: Bluetooth is OFF");
}else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.d(TAG, "onReceive: Started Discovery");
}else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d(TAG, "onReceive: Finished Discovery");
}else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// When discovery finds a device
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mBtDiscoveredList.add(device);
Log.d(TAG, "onReceive: Found device"+ device.getName() +" ::Address: "+device.getAddress()+" ::String: "+device.toString());
}
}
mBluetoothAdapter.startDiscovery();
mBluetoothAdapter.cancelDiscovery();
Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
Method createBondMethod = class1.getMethod("createBond");
Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
return returnValue.booleanValue();
unregisterReceiver(mReceiver);
mBluetoothAdapter.isDiscovering();
mBluetoothAdapter.cancelDiscovery();