BluetoothSocket: Server and Client connecting error in accepting request and sending request

寵の児 提交于 2019-12-25 08:41:43

问题


Before this, I had asked a question about the CLIENT side in stackoverflow.com , there is no solution to the client side yet. So I am thinking maybe I can show the server side, make a comparison between these two side. FYI, currently testing on an android version 6.0.1 device.

Below is my code(SERVER):

public class MainActivity extends AppCompatActivity {
    BluetoothAdapter mBluetoothAdapter;
    IntentFilter mIntentFilter;
    BroadcastReceiver mReceiver;
    Intent discoverableIntent;
    private BluetoothSocket socket;
    private static final int DISCOVERABLE_REQUEST_CODE = 0x1;
    public static final String NAME = "bluetoothComm";
    public static final UUID MY_UUID = UUID.fromString("a60f35f0-b93a-11de-8a19-03002011c456");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();

    }

    private void turnOnBluetooth() {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
    }

    public void init(){
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(MainActivity.this, "Bluetooth is not supported in this device", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            if (!mBluetoothAdapter.isEnabled()) {
                turnOnBluetooth();
            }
        }

        discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        startActivityForResult(discoverableIntent, DISCOVERABLE_REQUEST_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        android.util.Log.e("TrackingFlow", "Creating thread to start listening...");
        new Thread(reader).start();
    }

    private Runnable reader = new Runnable() {
        public void run() {
            try {
                BluetoothServerSocket serverSocket = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
                android.util.Log.e("TrackingFlow", "Listening...");
                socket = serverSocket.accept();
                android.util.Log.e("TrackingFlow", "Socket accepted...");
        }catch (Exception e){
                e.printStackTrace();
            }
        }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mReceiver);
    }

}

Above codes will give me no error, however, the bluetooth socket is not accepting new request, although it started to listen for incoming request. Please make a comparison between these two sides. I had spent so much time to figure the solution out...someone please help me!

来源:https://stackoverflow.com/questions/41339494/bluetoothsocket-server-and-client-connecting-error-in-accepting-request-and-sen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!