I know there are lots of already asked questions about the the title I provided above, but I found none as a satisfactory one. Also I gone through Android Bluetooth and its sample Bluetooth Chat application. But What I want is to create a demo android app, which receives data (String message or file) from any other Bluetooth enabled device (Either android or non-android both) Any links or code snippets for above will help a lot. Regards Appy Coding..
EDIT Following is the piece of code I am trying to get data after successfully selection of device from DeviceListPickerActivity.java:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(D) Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
device = mBluetoothAdapter.getRemoteDevice(address);
String deviceFriendlyName = device.getName().toString();
Toast.makeText(getApplicationContext(), "Device " + deviceFriendlyName +" Selected", Toast.LENGTH_SHORT).show();
try{
BluetoothSocket btSocket = device.createRfcommSocketToServiceRecord(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
btSocket.connect();
String strReceivedBytesFromDevice;
InputStream input = btSocket.getInputStream();
DataInputStream dinput = new DataInputStream(input);
StringBuffer inputLine = new StringBuffer();
while((strReceivedBytesFromDevice = dinput.readLine()) != null){
inputLine.append(strReceivedBytesFromDevice);
System.out.println("Data Received from BT device:" + strReceivedBytesFromDevice);
}
dinput.close();
}catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Error in establishing connection with device.",
Toast.LENGTH_SHORT).show();
}
// Attempt to connect to the device
if(mChatService != null){
mChatService.connect(device);
// getBluetoothMessageString();
}else {
// Initialize the BluetoothChatService to perform bluetooth connections
mChatService = new BluetoothService(this, mHandler);
// getBluetoothMessageString();
}
}
/*else{
Toast.makeText(getApplicationContext(), "Failed to connect to device", Toast.LENGTH_LONG).show();
}*/
break;