Created
September 25, 2013 13:48
-
-
Save sunnyone/6699876 to your computer and use it in GitHub Desktop.
Android Bluetooth SPP Client Sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final String command = "HELO\n"; | |
final UUID sppUuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); | |
BluetoothAdapter.getDefaultAdapter(); | |
if (btAdapter == null) { | |
sendLogMessage("Bluetooth adapter is not available."); | |
return; | |
} | |
sendLogMessage("Bluetooth adapter is found."); | |
if (!btAdapter.isEnabled()) { | |
sendLogMessage("Bluetooth is disabled. Check configuration."); | |
return; | |
} | |
sendLogMessage("Bluetooth is enabled."); | |
BluetoothDevice btDevice = null; | |
Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices(); | |
for (BluetoothDevice dev : bondedDevices) { | |
sendLogMessage("Paired device: " + dev.getName() + " (" + dev.getAddress() + ")"); | |
if (dev.getName().equals("MyDeviceName")) { | |
btDevice = dev; | |
} | |
} | |
if (btDevice == null) { | |
sendLogMessage("Target Bluetooth device is not found."); | |
return; | |
} | |
sendLogMessage("Target Bluetooth device is found."); | |
BluetoothSocket btSocket; | |
try { | |
btSocket = btDevice.createRfcommSocketToServiceRecord(sppUuid); | |
} catch (IOException ex) { | |
sendLogMessage("Failed to create RfComm socket: " + ex.toString()); | |
return; | |
} | |
sendLogMessage("Created a bluetooth socket."); | |
for (int i = 0; ; i++) { | |
try { | |
btSocket.connect(); | |
} catch (IOException ex) { | |
if (i < 5) { | |
sendLogMessage("Failed to connect. Retrying: " + ex.toString()); | |
continue; | |
} | |
sendLogMessage("Failed to connect: " + ex.toString()); | |
return; | |
} | |
break; | |
} | |
sendLogMessage("Connected to the bluetooth socket."); | |
try { | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(btSocket.getOutputStream(), "ASCII")); | |
writer.write(command); | |
writer.flush(); | |
} catch (IOException ex) { | |
sendLogMessage("Failed to write a command: " + ex.toString()); | |
return; | |
} | |
sendLogMessage("Command is sent: " + command); | |
String output; | |
try { | |
BufferedReader reader = new BufferedReader(new InputStreamReader(btSocket.getInputStream(), "ASCII")); | |
output = reader.readLine(); | |
} catch (IOException ex) { | |
sendLogMessage("Failed to write a command: " + ex.toString()); | |
return; | |
} | |
sendLogMessage("Result: " + output); | |
try { | |
btSocket.close(); | |
} catch (IOException ex) { | |
sendLogMessage("Failed to close the bluetooth socket."); | |
return; | |
} | |
sendLogMessage("Closed the bluetooth socket."); |
Could you please explain whey ASCII is used instead UTF-8 BufferedReader reader = new BufferedReader(new InputStreamReader(btSocket.getInputStream(), "ASCII"));
Not 100% sure, but my guess is that most serial protocols are byte-oriented, so you would want a byte-oriented encoding.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please explain whey ASCII is used instead UTF-8
BufferedReader reader = new BufferedReader(new InputStreamReader(btSocket.getInputStream(), "ASCII"));
Thanks!!!