Bluetooth ON/OFF and visible mode in java & sketchwere

Bluetooth ON

final android.bluetooth.BluetoothAdapter bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
    showMessage("This device support bluetooth");
} else {
   showMessage("This device not supported bluetooth");
}


if (!bluetoothAdapter.isEnabled()) {
Intent turnOn = new Intent((android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE));
startActivityForResult(turnOn, 0);
showMessage("Turned On");
} else {
showMessage("Already On");
}

Bluetooth OFF

final android.bluetooth.BluetoothAdapter bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
    showMessage("This device support bluetooth");
} else {
   showMessage("This device not supported bluetooth");
}


bluetoothAdapter.disable();
showMessage("OFF");

Make Bluetooth visible

final android.bluetooth.BluetoothAdapter bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
    showMessage("This device support bluetooth");
} else {
   showMessage("This device not supported bluetooth");
}


Intent getVisible = new Intent(android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); startActivityForResult(getVisible, 0);

}

private static final int BT_VISIBLE_ENABLE = 13;

private void makeBTDiscoverable() {
Intent discoverableIntent = new Intent(android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivityForResult(discoverableIntent, BT_VISIBLE_ENABLE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BT_VISIBLE_ENABLE) {
showMessage("The device has been made available successfully");
} else {
showMessage("An error was encountered while making the device discoverable!");
}

Paired Device list

final android.bluetooth.BluetoothAdapter bluetoothAdapter = android.bluetooth.BluetoothAdapter.getDefaultAdapter();
final Set<android.bluetooth.BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

final ArrayList list = new ArrayList();
for (android.bluetooth.BluetoothDevice bt : pairedDevices)
list.add(bt.getName()); 
showMessage("Showing Paired Devices");
final ArrayAdapter<android.content.pm.ResolveInfo> adapter = new ArrayAdapter<android.content.pm.ResolveInfo>(MainActivity.this, android.R.layout.simple_list_item_1, list);
listview1.setAdapter(adapter);
listview1.refreshDrawableState();

Add permission

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.