Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 蓝牙连接:不安全连接,安全连接,安全或不安全地监听,使用什么?_Android_Bluetooth_Connection - Fatal编程技术网

Android 蓝牙连接:不安全连接,安全连接,安全或不安全地监听,使用什么?

Android 蓝牙连接:不安全连接,安全连接,安全或不安全地监听,使用什么?,android,bluetooth,connection,Android,Bluetooth,Connection,我正努力在星形拓扑中获得一致的蓝牙连接。我有一个主手机是三星Galaxy S4运行API 10。所有连接到S4上bluetoothserver插座的手机都是同样运行API 10的LG Dynamic Tracfone 在过去的几天里,我在网上看到了很多关于使用何种连接的相互矛盾的信息 这是我当前的设置: 主代码 public void acceptConnection() { .... (enable bt adapter) ... // initializes a Bluetoot

我正努力在星形拓扑中获得一致的蓝牙连接。我有一个主手机是三星Galaxy S4运行API 10。所有连接到S4上bluetoothserver插座的手机都是同样运行API 10的LG Dynamic Tracfone

在过去的几天里,我在网上看到了很多关于使用何种连接的相互矛盾的信息

这是我当前的设置:

主代码

public void acceptConnection() {

.... (enable bt adapter) ...

    // initializes a Bluetooth server socket
    bluetoothServerSocket = bc.createBluetoothServerSocket();
    //connection made to Master, discovery no longer needed
    bluetoothAdapter.cancelDiscovery();

    BluetoothSocket bluetoothSocket;

    // loops until the thread is interrupted or an exception occurs
    while (!isInterrupted()) {

        try {
            // attempts to accept the slave application's connection
            bluetoothSocket = bluetoothServerSocket.accept();
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
            Log.v("Default Thread", "Connection to slave failed.");
            // breaks out of the while loop
            return;
        }

        try {
            ... (enumerate all input and output streams, and all bt sockets) ...
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
        }
    }
这就是创建blueToothServerSocket时调用的方法,这就是我一半的困惑所在。我应该如何监听适配器?目前,我做这件事很不安全

public BluetoothServerSocket createBluetoothServerSocket() {

    // gets the name of the application
    String name = "PVCED";
    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initializes an empty Bluetooth server socket
    serverSocket = null;

    try {
        // creates a Bluetooth socket using a common UUID
        serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, uuid);
    } catch (IOException e) {
        // prints out the exception's stack trace
        e.printStackTrace();
    }

    return serverSocket;
}
private BluetoothSocket createBluetoothSocket(Set<BluetoothDevice> pairedDevices) {

    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initialises an empty Bluetooth socket
    BluetoothSocket bluetoothSocket = null;

    // checks to see if there are any paired devices
    if (pairedDevices.size() > 0) {
        // loops through each paired device
        for (BluetoothDevice device : pairedDevices) {
            // checks to see if the name of the paired device is MASTER
            if (device.getName().equals("MASTER")) {
                try {
                    master = device;
                    // creates a Bluetooth socket using a common UUID
                    //bluetoothSocket = master.createRfcommSocketToServiceRecord(uuid);
                    //Method m = master.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class});
                    //bluetoothSocket = (BluetoothSocket) m.invoke(master, 1);
                    bluetoothSocket = master.createInsecureRfcommSocketToServiceRecord(uuid);
                } catch(Exception e){
                    Log.v("Connect Exception", e.getMessage());
                }
            }
        }
    }
    //check if we paired succesfully to a master, if not, prompt user to do so.
    if (master == null){
        ... (tell user to pair with master via toast) ...
    }

    return bluetoothSocket;
}
从代码

public void acceptConnection() {

.... (enable bt adapter) ...

    // initializes a Bluetooth server socket
    bluetoothServerSocket = bc.createBluetoothServerSocket();
    //connection made to Master, discovery no longer needed
    bluetoothAdapter.cancelDiscovery();

    BluetoothSocket bluetoothSocket;

    // loops until the thread is interrupted or an exception occurs
    while (!isInterrupted()) {

        try {
            // attempts to accept the slave application's connection
            bluetoothSocket = bluetoothServerSocket.accept();
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
            Log.v("Default Thread", "Connection to slave failed.");
            // breaks out of the while loop
            return;
        }

        try {
            ... (enumerate all input and output streams, and all bt sockets) ...
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
        }
    }
这就是我困惑的另一半,我应该如何创建套接字?目前我做这件事很不安全

public BluetoothServerSocket createBluetoothServerSocket() {

    // gets the name of the application
    String name = "PVCED";
    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initializes an empty Bluetooth server socket
    serverSocket = null;

    try {
        // creates a Bluetooth socket using a common UUID
        serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, uuid);
    } catch (IOException e) {
        // prints out the exception's stack trace
        e.printStackTrace();
    }

    return serverSocket;
}
private BluetoothSocket createBluetoothSocket(Set<BluetoothDevice> pairedDevices) {

    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initialises an empty Bluetooth socket
    BluetoothSocket bluetoothSocket = null;

    // checks to see if there are any paired devices
    if (pairedDevices.size() > 0) {
        // loops through each paired device
        for (BluetoothDevice device : pairedDevices) {
            // checks to see if the name of the paired device is MASTER
            if (device.getName().equals("MASTER")) {
                try {
                    master = device;
                    // creates a Bluetooth socket using a common UUID
                    //bluetoothSocket = master.createRfcommSocketToServiceRecord(uuid);
                    //Method m = master.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class});
                    //bluetoothSocket = (BluetoothSocket) m.invoke(master, 1);
                    bluetoothSocket = master.createInsecureRfcommSocketToServiceRecord(uuid);
                } catch(Exception e){
                    Log.v("Connect Exception", e.getMessage());
                }
            }
        }
    }
    //check if we paired succesfully to a master, if not, prompt user to do so.
    if (master == null){
        ... (tell user to pair with master via toast) ...
    }

    return bluetoothSocket;
}
private BluetoothSocket createBluetoothSocket(设置pairedDevices){
//获取主应用程序和从应用程序的公共UUID
UUID UUID=UUID.fromString(“23ea856c-49da-11e4-9e35-164230d1df67”);
//初始化空的蓝牙套接字
BluetoothSocket BluetoothSocket=null;
//检查是否有任何配对设备
如果(pairedDevices.size()>0){
//循环通过每个配对设备
用于(蓝牙设备:pairedDevices){
//检查配对设备的名称是否为主设备
if(device.getName().equals(“MASTER”)){
试一试{
主设备=设备;
//使用公共UUID创建蓝牙套接字
//bluetoothSocket=master.createrFComSocketToServiceRecord(uuid);
//方法m=master.getClass().getMethod(“CreateRfcomsockettoServiceRecord”,新类[]{int.Class});
//bluetoothSocket=(bluetoothSocket)m.invoke(master,1);
bluetoothSocket=master.createUnsecurerCommsocketToServiceRecord(uuid);
}捕获(例外e){
Log.v(“连接异常”,例如getMessage());
}
}
}
}
//检查我们是否成功地与主机配对,如果没有,则提示用户进行配对。
if(master==null){
…(告诉用户通过toast与master配对)。。。
}
返回蓝牙插座;
}
我的logcat经常充满错误,例如“文件描述符不正确”、“无法启动服务发现”或“服务发现失败”

对于我的场景,最好的连接方案是什么?如果你们需要更多关于我如何启用/禁用bt适配器或关闭bt连接的详细信息,我可以提供更多代码