Я новичок в программировании под Android, и теперь у меня проблема. Я хочу использовать свой телефон Android в качестве сервера Bluetooth, а это означает, что когда я открываю специальное действие, телефон должен прослушивать другие устройства Bluetooth (которые уже сопряжены), а другое устройство может установить соединение с моим телефоном Android. Так должно быть, как будто телефон - это периферийное устройство, как динамик bt. Так что спаривание все еще выполнено. И выяснил, что мне нужно использовать режим SPP. Когда он подключается, я хочу отправить простые байтовые потоки. Я нашел приложение под названием 'bluetooth spp tools pro' (https://play.google.com/store/apps/details?id=mobi.dzs.android.BLE_SPP_PRO&hl=de), который делает почти все, что я хочу. Но тут проблема в том, что телефоны работают как клиентские и открывают соединение.
Так что, возможно, вы могли бы мне помочь и дать мне несколько советов, чтобы понять, что мне нужно делать.
Спасибо за помощь:)
РЕДАКТИРОВАТЬ: Теперь я попробовал пример из Android, и кажется, что он почти работает. Я могу подключить свой компьютер к смартфону, и смартфон подключается к нему. Но когда оно подключилось, приложение вылетает, и я не знаю, почему ... может быть, вы мне скажете, почему приложение выходит из строя ... вот код:
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) { }
}
}
}