Professional Documents
Culture Documents
Android Bluetooth Connection
Android Bluetooth Connection
Bluetooth Overview:
The Android platform includes support for the Bluetooth network stack, which allows
a device to wirelessly exchange data with other Bluetooth devices. The application
framework provides access to the Bluetooth functionality through the Android
Bluetooth APIs. These APIs let applications wirelessly connect to other Bluetooth
devices, enabling point-to-point and multipoint wireless features.
Using the Bluetooth APIs, an Android application can perform the following:
According to this, we can "build" a local area network (LAN) by connecting devices
over Bluetooth. The Android platform includes support for the Bluetooth network
stack, which allows a device to wirelessly exchange data with other Bluetooth
devices.
The 2nd important works is check if your device is enabled Bluetooth. If not,
request to turn it on:
if (bluetoothAdapter.isDiscovering())
{ bluetoothAdapter.cancelDiscovery();
} bluetoothAdapter.startDiscovery();
if (device.getBondState() != BluetoothDevice.BOND_BONDED)
{ discoveredDevicesAdapter.add(device.getName() + "\n" + device.getAddress());
} } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (discoveredDevicesAdapter.getCount() == 0)
{ discoveredDevicesAdapter.add(getString(R.string.none_found)); } } } };
Moreover, your devices can be connected to some other devices before, so you can
listing them by call getBondedDevices():
if (pairedDevices.size() > 0)
} else
pairedDevicesAdapter.add(getString(R.string.none_paired));
To connect two devices, we must implement server side and client side mechanism.
One device shall open the server socket and another should initiate the connection
(the remain device is a client device).
With connection as server:
Initializing an instance of BluetoothServerSocket by calling
the listenUsingRfcommWithServiceRecord() method.
public AcceptThread()
{ ex.printStackTrace();
}
serverSocket = tmp;
{ setName("AcceptThread");
BluetoothSocket socket;
{ try
{ socket = serverSocket.accept();
} catch (IOException e)
{ break;
if (socket != null)
{ synchronized (ChatController.this)
{ switch (state)
{ case STATE_LISTEN:
break;
try {
Connected as a client:
BluetoothDevice object.
{ tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e)
{ e.printStackTrace();
socket = tmp;
{ setName("ConnectThread");
} catch (IOException e)
{ try
{ socket.close();
} connectionFailed(); return;
{ connectThread = null; }
try {
} catch (IOException e)
{ }
inputStream = tmpIn;
outputStream = tmpOut; }
int bytes;
try {
} catch (IOException e)
{ connectionLost();
ChatController.this.start();
break; } } }
// write to OutputStream
public void write(byte[] buffer)
{ try
{ try
{ bluetoothSocket.close();
} catch (IOException e)
{ e.printStackTrace();
} } }
Application output
For reference:
https://developer.android.com/guide/topics/connectivity/bluetooth
http://www.devexchanges.info/2016/10/simple-bluetooth-chat-application-in.html