连接到蓝牙设备,异常:服务发现失败



我正在尝试将我的安卓设备与蓝牙兼容设备连接。我知道这个设备的 mac 地址,正如你在下面的代码中看到的那样。我还做了几个Toasts,只是为了验证代码中的步骤。似乎我设法创建了我假设tmp = device.createRfcommSocketToServiceRecord(MY_UUID);因为 Toast 在以下代码行中会引发一条消息,说一些蓝牙对象地址:

String test = tmp.toString(); 
Toast.makeText(getApplicationContext(), "The bluetooth socket: " +test, Toast.LENGTH_LONG).show(); 

但是当我在 API 级别 15、android 4.0 上工作时tmp.connect();代码在语义上失败

这是我的代码段

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;  
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SimpleConnectAndroidActivity extends Activity {
final static String toast = "IAM HERE"; 
final static String TAG ="SimpleConnect";
UUID MY_UUID;
BluetoothDevice bd;
BluetoothAdapter ba;
Button connectButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    ba = BluetoothAdapter.getDefaultAdapter();
    connectButton = (Button)findViewById(R.id.button1);
    connectButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            boolean b = ba.checkBluetoothAddress("00:1B:DC:0F:EC:7E");
            BluetoothSocket tmp = null; 
            //If valid bluetoothAddress
            if(b) {
                final BluetoothDevice device = ba.getRemoteDevice("00:1B:DC:0F:EC:7E"); //Getting the Verifier Bluetooth 
                //Trying to create a RFCOMM socket to the device
                try {
                    tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
                } catch (IOException e) {
                }
                try {
                    tmp.connect();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "No connection was made! Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
                } 
                //Print out the sockets name
                String test = tmp.toString(); 
                Toast.makeText(getApplicationContext(), "The bluetooth socket: " +test, Toast.LENGTH_LONG).show(); 
            }

            else {
                    Toast.makeText(getApplicationContext(), "FALSE, No bluetooth device with this address", Toast.LENGTH_LONG).show();
                }
            }   
    });  
}

}

我知道这应该在单独的线程中完成,是的,我阅读了谷歌提供的蓝牙聊天示例。

有人可以帮助我吗?

编辑

这是我从 LogCat 得到的唯一东西:

07-06 10:10:22.085: V/BluetoothSocket.cpp(14019): ...fd 63 created (RFCOMM, lm = 26)

编辑 2

好的,现在有一个新错误,但这是一个更好的错误。当我按下连接按钮时,手机会询问配对设备的 PIN 码,但它已经配对了!第二次配对后,错误显示:连接被拒绝,另一个异常显示超时。

您可以使用以下代码: Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}); mySocket = (BluetoothSocket) m.invoke(device, 1);

相关内容

最新更新