Android 不安全的蓝牙连接

Posted

技术标签:

【中文标题】Android 不安全的蓝牙连接【英文标题】:Android insecure bluetooth connection 【发布时间】:2015-05-02 17:26:45 【问题描述】:

我想将 android mobile 连接到一个电子设备,但我想不安全地连接它。但是它给了我错误“java.io.IOException:读取失败,套接字可能关闭或超时,读取 ret:-1 "

这是我的代码

包 net.londatiga.android.bluetooth;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

public class ConnectThread extends Thread 
    private final BluetoothSocket mmSocket;

    private final UUID WELL_KNOWN_UUID = UUID
            .fromString("00001101-0000-1000-8000-00805F9B34FB");

    public ConnectThread(BluetoothDevice device) 
        // Use a temporary object that is later assigned to mmSocket,because
        // mmSocket is final
        BluetoothSocket tmp = null;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try 
            // tmp = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            tmp = device
                    .createRfcommSocketToServiceRecord(WELL_KNOWN_UUID);
            Object[] params = new Object[] Integer.valueOf(1);
            // This is the trick
            Method m = device.getClass().getMethod("createInsecureRfcommSocket",
                    new Class[]  int.class );
            tmp = (BluetoothSocket) m.invoke(device, params);
         catch (Exception e) 
            e.printStackTrace();
        

        mmSocket = tmp;
    

    @SuppressLint("NewApi")
    public void run() 
        // DebugLog.i(TAG, "Trying to connect...");
        // Cancel discovery because it will slow down the connection
        MainActivity.mBluetoothAdapter.cancelDiscovery();

        try 
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();

            boolean val=mmSocket.isConnected();
            Log.v("val", ""+val);
            // DebugLog.i(TAG, "Connection stablished");
         catch (IOException connectException) 
            // Unable to connect; close the socket and get out
            // DebugLog.e(TAG, "Fail to connect!", connectException);
            try 
                mmSocket.close();
             catch (IOException closeException) 
                // DebugLog.e(TAG, "Fail to close connection", closeException);
            
            return;
        
    

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() 
        try 
            mmSocket.close();
         catch (IOException e) 
        
    

【问题讨论】:

【参考方案1】:

您必须实现另一个线程,如下所示,它始终接收您的调用并执行操作。启动应用程序时启动此线程。

class AcceptThread extends
 Thread 
    private final BluetoothServerSocket serverSocket;
    private boolean flag = true;

    public AcceptThread() 
        BluetoothServerSocket tmp = null;

        try 
            tmp = bluetoothAdapter
                    .listenUsingInsecureRfcommWithServiceRecord(
                            NAME_UUID, uuid);

         catch (IOException e) 
        
        serverSocket = tmp;
    

    public void run() 
        BluetoothSocket socket = null;
        while (true) 
            try 
                socket = serverSocket.accept();
                if (socket.isConnected()) 
                    Log.d(TAG, "run:  connection successfull");
                    Log.d(TAG, "run: " +     socket.getRemoteDevice().getName() + "  " +
                            socket.getRemoteDevice().getAddress());
                

             catch (IOException e) 

                try 
                    socket.close();
                 catch (Exception eee) 
                    Log.d(TAG, "run: " + eee.getMessage());
                
                break;
            
        
    

    public void cancel() 
        try 
            serverSocket.close();
         catch (IOException e) 
        
    

【讨论】:

以上是关于Android 不安全的蓝牙连接的主要内容,如果未能解决你的问题,请参考以下文章

使用不安全的 RFCOMM 蓝牙套接字时,android 何时显示配对对话框?

Android蓝牙开发—— 经典蓝牙连接方法

android的蓝牙匹配连接

android adb 支持蓝牙连接吗

为啥android6.0的手机不能连接蓝牙

Android怎么检测蓝牙的连接状态?如果一段断开,我这边怎么检测得到?