自动连接蓝牙持续连接的设备蓝牙、设备

2023-09-07 03:32:49 作者:爱笑的女生

我使用的蓝牙聊天样品和其完美配对的设备,并从一个设备发送文本到另一个,但有时它断开,我想再次与上次连接的设备重新连接。如何做我做到这一点。我有细试图从自动连接内从蓝牙Play商店,但它连接耳机和从应用程序之外没有。

我如何从应用程序内实现这一目标?

先谢谢了。

  E / BluetoothChatService(10175):接受()失败E / BluetoothChatService(10175):java.io.IOException异常:操作已取消E / BluetoothChatService(10175):在android.bluetooth.BluetoothSocket.acceptNative(本机方法)E / BluetoothChatService(10175):在android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:311)E / BluetoothChatService(10175):在android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:107)E / BluetoothChatService(10175):在android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:93)E / BluetoothChatService(10175):在com.example.android.BluetoothChat.BluetoothChatService$AcceptThread.run(BluetoothChatService.java:276) E / BluetoothChatService(10175):断开 E / BluetoothChatService(10175):java.io.IOException异常:软件导致连接中断 E / BluetoothChatService(10175):在android.bluetooth.BluetoothSocket.readNative(本机方法) E / BluetoothChatService(10175):在android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333) E / BluetoothChatService(10175):在android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) E / BluetoothChatService(10175):在java.io.InputStream.read(InputStream.java:163) E / BluetoothChatService(10175):在com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:436) E / AndroidRuntime(10175):致命异常:螺纹1274 E / AndroidRuntime(10175):显示java.lang.NullPointerException E / AndroidRuntime(10175):在com.example.android.BluetoothChat.BluetoothChatService.connectionLost(BluetoothChatService.java:242) E / AndroidRuntime(10175):在com.example.android.BluetoothChat.BluetoothChatService.access $ 6(BluetoothChatService.java:221) E / AndroidRuntime(10175):在com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:443)E/BluetoothChat(10175): -  ON PAUSE  -  

解决方案

写到这使用gedit的,因为我没有日食大气压。所以,如果有一些编码错误,请不要犹豫,编辑。为了便于说明,我没有使用的共享preferences这里,如果你想使用不要犹豫了。

  //假设你有设备地址和连接私人字符串partnerDevAdd =00:11:22:AA:BB:CC;私人布尔isConnected = TRUE;IntentFilter的过滤器=新的IntentFilter();filter.addAction(BluetoothDevice.ACTION_FOUND);filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);registerReceiver(mReceiver,过滤器);//创建蓝牙相关检查一个BroadcastReceiver私人最终的BroadcastReceiver mReceiver =新的广播接收器(){    公共无效的onReceive(上下文的背景下,意图意图){        字符串行动= intent.getAction();        //我们不想重新连接到已连接的设备        如果(isConnected == FALSE){            //当发现找到的设备            如果(BluetoothDevice.ACTION_FOUND.equals(动作)){                //从意向获取BluetoothDevice类对象                BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);                //检查发现的设备是我们不得不与通讯                如果(device.getAddress()。等于(partnerDevAdd)==真)                    connectToExisting(设备);            }        }        如果(BluetoothDevice.ACTION_ACL_CONNECTED.equals(动作)){            //从意向获取BluetoothDevice类对象            BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);            //检查连接的设备是我们不得不与通讯            如果(device.getAddress()。等于(partnerDevAdd)==真)                isConnected = TRUE;        }否则如果(BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(动作)){            //从意向获取BluetoothDevice类对象            BluetoothDevice类设备= intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);            //检查连接的设备是我们不得不与通讯            如果(device.getAddress()。等于(partnerDevAdd)==真)                isConnected = FALSE;        }    }};私人无效connectToExisting(BluetoothDevice类设备){    新ConnectThread(设备);} 

ConnectThread可以在这里找到的http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient

蓝牙 不只是连接,也是智能时代的 标配

I am using bluetooth chat sample and its perfectly fine with pairing the devices and sending text from one device to another but sometimes it disconnects and I would like to reconnect with the last connected device again .How do I achieve this .I have tried auto connect bluetooth from play store but it connects headset and from outside the app not from within.

How do I achieve this from within the App?

Thanks in Advance.

E/BluetoothChatService(10175): accept() failed
E/BluetoothChatService(10175): java.io.IOException: Operation Canceled
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:311)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:107)
E/BluetoothChatService(10175):  at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:93)
E/BluetoothChatService(10175):  at com.example.android.BluetoothChat.BluetoothChatService$AcceptThread.run(BluetoothChatService.java:276)




 E/BluetoothChatService(10175): disconnected
 E/BluetoothChatService(10175): java.io.IOException: Software caused connection abort
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothSocket.readNative(Native Method)
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
 E/BluetoothChatService(10175):     at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
 E/BluetoothChatService(10175):     at java.io.InputStream.read(InputStream.java:163)
 E/BluetoothChatService(10175):     at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:436)



 E/AndroidRuntime(10175): FATAL EXCEPTION: Thread-1274
 E/AndroidRuntime(10175): java.lang.NullPointerException
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService.connectionLost(BluetoothChatService.java:242)
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService.access$6(BluetoothChatService.java:221)
 E/AndroidRuntime(10175):   at com.example.android.BluetoothChat.BluetoothChatService$ConnectedThread.run(BluetoothChatService.java:443)E/BluetoothChat(10175): - ON PAUSE -

解决方案

Wrote this using gEdit since, I don't have eclipse atm. So if there is some coding error please don't hesitate to edit this. For ease of explanation I haven't used SharedPreferences here, if you want to use don't hesitate it.

//Assuming that you have device address and is connected
private String partnerDevAdd="00:11:22:AA:BB:CC";
private boolean isConnected=true;

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

registerReceiver(mReceiver, filter);

// Create a BroadcastReceiver for bluetooth related checks
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        //We don't want to reconnect to already connected device
        if(isConnected==false){
            // When discovery finds a device
            if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                // Check if the found device is one we had comm with
                if(device.getAddress().equals(partnerDevAdd)==true)
                    connectToExisting(device);
            }
        }

        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Check if the connected device is one we had comm with
            if(device.getAddress().equals(partnerDevAdd)==true)
                isConnected=true;
        }else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // Check if the connected device is one we had comm with
            if(device.getAddress().equals(partnerDevAdd)==true)
                isConnected=false;
        }
    }
};

private void connectToExisting(BluetoothDevice device){
    new ConnectThread(device);
}

ConnectThread is available here http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingAsAClient