Android Bluetooth Example

Android sample bluetooth code to send a simple string via bluetooth

private OutputStream outputStream;
private InputStream inStream;

private void init() throws IOException {
BluetoothAdapter blueAdapter = BluetoothAdapter.getDefaultAdapter();
if (blueAdapter != null) {
if (blueAdapter.isEnabled()) {
Set<BluetoothDevice> bondedDevices = blueAdapter.getBondedDevices();

if(bondedDevices.size() > 0) {
Object[] devices = (Object []) bondedDevices.toArray();
BluetoothDevice device = (BluetoothDevice) devices[position];
ParcelUuid[] uuids = device.getUuids();
BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream = socket.getOutputStream();
inStream = socket.getInputStream();
}

Log.e("error", "No appropriate paired devices.");
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}

public void write(String s) throws IOException {
outputStream.write(s.getBytes());
}

public void run() {
final int BUFFER_SIZE = 1024;
byte[] buffer = new byte[BUFFER_SIZE];
int bytes = 0;
int b = BUFFER_SIZE;

while (true) {
try {
bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Bluetooth Examples for Android

From what I have gathered, the distinction: server and client exists only while the Bluetooth connection is being established (ie during the discovery and pairing process). For the connection to be established, one device acts as a server (using an instance of BluetoothServerSocket class) and the other acts as a client (using an instance of the BluetoothSocket class). The (acting) server listens for incoming requests and the client requests listening servers to connect. After the connection is established (see the details of the methods used on the Android Dev Guide), both the (initially called) server and client interact using the BluetoothSocket object only. So no such server/client distinction exists.

You can check out the code of the Bluetooth Chat example on the Dev Guide, particularly of the BluetoothChatService class. Call to the method createRfcommSocketToServiceRecord() returns a BluetotohSocket to the listening(server) device. The requesting device(client) as it is uses a similar object.

True, further example codes would be nicer.

Android 12 New Bluetooth Permissions

This was a platform bug. Google fixed the bug in the new Android 12 Beta version.

Cant connect to device using bluetooth chat example

I I found where is the problem. The unique ID for my application was wrong. I found that uuid on https://bellcode.wordpress.com/2012/01/02/android-and-arduino-bluetooth-communication/ By the way I don't understand why this uuid works other don't if someone could simply explain what thats about I would be grateful.

// Unique UUID for this application
private static final UUID MY_UUID_SECURE =
UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
private static final UUID MY_UUID_INSECURE =
UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");


Related Topics



Leave a reply



Submit