Reputation: 309
I'm new in Android programming and i now have a problem. I want to use my android phone as a bluetooth server, which means that when I open a special Activity the phone should listen to other Bluetooth devices (which are already paired) and the other device can open a conncection to my android phone. So it should be like the phone is a pheripheral device, like a bt speaker. So the pairing is still done. And in found out that I have to use the SPP mode. When it's connect i want to send an receive simple byte streams. I found a app called 'bluetooth spp tools pro' (https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=de) which does almost everything i want. But here the problem is that the phones works as client and opens the connection.
So maybe you could help me and give me some tips to understand what i have to do.
Thanks for the help:)
EDIT: Now I tried a Example from Android and It seems like it is almost working. I can connect my pc with the Smartphone and the smartphone connects with it. But when it has connected, the app crashes and I don't know why... maybe you can tell me why ma app crashes.. here the code:
public class BluetoothServer extends AppCompatActivity {
public BluetoothAdapter mBluetoothAdapter;
private static final UUID MY_UUID_SECURE = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private AcceptThread mSecureAcceptThread;
private TextView textViewStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth_server);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
textViewStatus = (TextView) findViewById(R.id.BluetoothServerTextView);
}
@Override
protected void onDestroy() {
super.onDestroy();
mSecureAcceptThread.cancel();
mSecureAcceptThread = null;
}
public void onClickOpenBluetoothServer(View view) {
// Setup Bluetooth Adapter
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//Enable Discoverbility
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
textViewStatus.append("\nDiscoverable Added!");
//Starting Accepting Thread
mSecureAcceptThread = new AcceptThread();
mSecureAcceptThread.start();
}
/**
* Accepting Thread mit run();
*/
private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
public AcceptThread() {
BluetoothServerSocket tmp = null;
try {
// MY_UUID is the app's UUID string, also used by the client code
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("PAKSAFE", MY_UUID_SECURE);
} catch (IOException e) { }
mmServerSocket = tmp;
textViewStatus.append("\nAcceptThread Created!");
}
public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
textViewStatus.append("\nListening");
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
//manageConnectedSocket(socket);
textViewStatus.setText("Acccepted");
try {
mmServerSocket.close();
} catch (IOException e) {
break;
}
break;
}
}
}
/** Will cancel the listening socket, and cause the thread to finish */
public void cancel() {
try {
mmServerSocket.close();
} catch (IOException e) { }
}
}
}
Upvotes: 3
Views: 8020