I have been trying to connect Google Glass and to Nexus device using Bluetooth RFCOMM in which the app running on Nexus device acts as server and The Glass tries to connect as a client to the same UUID.
The answer : https://stackoverflow.com/posts/20414642/ is quite helpful, although I'm unable to get the desired functionality. There are no issues on connectivity part, but essentially I want to a) Transfer some data to Android Device b) Get back the response to Glass. So it's a two way communication I want to have.
I followed the Bluetooth guide on Android Developers site, and I think the modifications are required on the way handler works for managing the existing connection.
Handler on Glass App
public Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Log.i(TAG, "in handler");
super.handleMessage(msg);
switch(msg.what){
case SUCCESS_CONNECT:
//read and write data from remote device
unregisterReceiver(mReceiver);
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
connectedThread.start();
Toast.makeText(getApplicationContext(), "CONNECTED", 2).show();
//setContentView(R.layout.activity_main);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
int bufferContent = ByteBuffer.wrap(readBuf).getInt();
String string = new String(readBuf);
break;
}
}
};
run() function for ConnectThread running on Glass App
public void run() {
// Cancel discovery because it will slow down the connection
mBluetoothAdapter.cancelDiscovery();
Log.i(TAG, "connect - run");
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mmSocket.connect();
Log.i(TAG, "connect - succeeded");
} catch (IOException connectException) {
Log.i(TAG, "connect failed");
// Unable to connect; close the socket and get out
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
// Do work to manage the connection, the handler will send the connecting successful message with the socket back to the pool
mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
// manage connected socket
// manageConnectedSocket(mmSocket);
}
socket Manage connection running on Android Device
private void manageConnectedSocket(BluetoothSocket socket) {
// start our connection thread
mConnectedThread = new ConnectedThread(socket);
mConnectedThread.start();
// Send the name of the connected device back to the UI Activity
// so the HH can show you it's working and stuff...
String devs = "";
for (BluetoothSocket sock : mSockets) {
devs += sock.getRemoteDevice().getName() + "\n";
}
// pass it to the pool....
Message msg = handle.obtainMessage(STATE_CONNECTION_STARTED);
Bundle bundle = new Bundle();
bundle.putString("NAMES", devs);
msg.setData(bundle);
handle.sendMessage(msg);
}
The naming scheme for methods follows the ones mentioned on official documentation - http://developer.android.com/guide/topics/connectivity/bluetooth.html
My question would be, what are the changes required in these methods / or any other additional methods that would permit to transfer text of a TextView from Glass to textView of Nexus Device and back.