XJQ124 / Some-notes

本仓库记录一些电脑方面的小技巧,包含各个方面
0 stars 0 forks source link

查询部分完成,目前进行到连接部分 #4

Open XJQ124 opened 10 months ago

XJQ124 commented 10 months ago

任务:安卓蓝牙APP


1、把发现和启用可检测性部分做完了

基于上周的内容,增加启用可检测性的方法

  //启动可检测的方法
    public void startBluetoothDiscoverable(View view) {
        if (checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
            // 请求蓝牙管理权限
            requestPermissions(new String[]{Manifest.permission.BLUETOOTH_ADMIN}, REQUEST_ENABLE_BT);
        } else {
            int requestCode = 1;
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivityForResult(discoverableIntent, requestCode);
        }
    }

这里是将本设备开启被检测权限300s

下午本来想一步到位,我以为在app外部进行蓝牙配对成功以后,可以直接写连接控制的方法,结果我试了下来是行不通的。

只能老老实实的一步一步,这里我本来想在发现部分把扫描到的设备通过RecylcerView的方法展示出来的,后面想了一下没有必要

2、连接部分

由于是两个APP,我在TV 端作为服务器,然后进行连接

    //作为服务器连接
    private class AcceptThread extends Thread {
        private final BluetoothServerSocket mmServerSocket;
        private static final String NAME = "YourBluetoothServerName";
        private static final String TAG = "MyBluetoothServer";

        public AcceptThread() {
            // Use a temporary object that is later assigned to mmServerSocket
            // because mmServerSocket is final.
            BluetoothServerSocket tmp = null;
            try {
                // MY_UUID is the app's UUID string, also used by the client code.
                tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "Socket's listen() method failed", e);
            }
            mmServerSocket = tmp;
        }

        public void run() {
            BluetoothSocket socket = null;
            // Keep listening until exception occurs or a socket is returned.
            while (true) {
                try {
                    socket = mmServerSocket.accept();
                } catch (IOException e) {
                    Log.e(TAG, "Socket's accept() method failed", e);
                    break;
                }

                if (socket != null) {
                    // A connection was accepted. Perform work associated with
                    // the connection in a separate thread.
                    manageMyConnectedSocket(socket);
                    try {
                        mmServerSocket.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }
            }
        }

        private void manageMyConnectedSocket(BluetoothSocket socket) {
        }

        // Closes the connect socket and causes the thread to finish.
        public void cancel() {
            try {
                mmServerSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "Could not close the connect socket", e);
            }
        }
    }

问题就是出在这里了,还在排查,,,

明日任务:把连接做好,争取实现数据传输成功