Hi,
I have bought myself a Zephyr HxM BT device and I am trying to listen to its messages in a simple application. I have paired the Device to my HTC Legend and I can establish a connection but everytime I call the read() or available() method on my InputStream I get a NullPointerException.
Has anyone succeed in getting information off this device? I have tried to establish the connection by reflection on the "createRfcommSocket" method but this throws a MethodNotFound Exception on my device. Any help would be appreciated. Thanks, Dennis
below is my Listener class
/**
*
*/
class HXMListener{
HXMListener(BluetoothDevice device) {
this.device = device;
this.open = true;
if (device != null){
createSocket(device);
}
}
/**
* @param device
*/
private void createSocket(BluetoothDevice device) {
try {
socket = device.createRfcommSocketToServiceRecord(HXM_UUID);
in = socket.getInputStream();
/**
* BluetoothDevice hxm =
* BluetoothAdapter.getDefaultAdapter().getRemoteDevice
* ("00:07:80:97:62:a3"); Method m =
* hxm.getClass().getMethod("createRfcommSocket", new
* Class[]{int.class}); socket = (BluetoothSocket)m.invoke(hxm,
* Integer.valueOf(1)); socket.connect();
*/
} catch (IOException e) {
Log.d(TAG,
"Unable to open Socket streams for " + socket);
writeToHXMFile("Unable to open Socket streams for " + socket);
// tidy up the socket
try {
if (socket != null) {
socket.close();
}
} catch (IOException e1) {
Log.d(TAG,
"Unable to close Socket for " + socket);
}
}
}
private void connectionFailed() {
Log.d(TAG, "Connection Failed");
writeToHXMFile("Connection Failed");
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
boolean connected = false;
while (!connected && open) {
try {
socket.connect();
connected = true;
} catch (IOException e) {
connectionFailed();
Log.d(TAG, "Failed", e);
// Close the socket
try {
socket.close();
} catch (IOException e2) {
Log.d(
TAG,
"unable to close() "
+ "socket during connection failure"
,e);
}
}
finally{
createSocket(device);
}
}
byte[] b = new byte[1];
while(open){
writeToHXMFile("Listening...");
try {
}
//now we have the message lets read it to this
String s = "";
// while(open && in.available()>0){
s+= in.read(b);
// }
//now we have read it all
Log.d(TAG+"_INPUT", s);
writeToHXMFile("COMPLETE "+ s);
} catch (IOException e) {
Log.d(TAG, "IO Exception ", e);
writeToHXMFile("IO Excep" + e);
open = false;
}
}
try {
in.close();
socket.close();
} catch (IOException e) {
in = null;
socket = null;
}
}
private void writeToHXMFile(final String string) {
runOnUiThread(new Runnable() {
@Override
public void run() {
bluetoothMessageBox.setText(string);
Log.d("SOCKET MESSAGE",string);
}
});
}
public void close() {
open = false;
}
}