蓝牙连接到需要PIN的嵌入式设备

时间:2014-03-27 09:00:13

标签: android

我正在编写和Android应用程序,它将检测并连接到已知的嵌入式设备。我的问题是,每当我尝试连接时,它都会引发异常:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            try {
            device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
             Toast.makeText(getApplicationContext(),"Try Connecting to device : " + device.getName(),
                      Toast.LENGTH_LONG).show();
               byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
                BluetoothDevice.class.getMethod("setPin", byte[].class).invoke(device, pin);
                BlueToothConnect(device);
            }catch(Exception e){}
       }
    }
};

我的BlueToothConnect方法是:

public void BlueToothConnect(BluetoothDevice device){
            if (deviceName.equals(device.getName()))
            {
                 try
                 {

                //mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
                    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
                 mSocket = (BluetoothSocket) m.invoke(device, 1);

                     mSocket.connect();
                    GetConnectionDetails();
                     result = device;
                     blueTooth_On_off = 1;
                 }
                catch(Exception  e){
                    text.setText(getStackTrace(e));
                try{

                    result = null;
                    mSocket.close();
                    blueTooth_On_off = 0;
                }
                catch(IOException e1){
                    result = null;
                    blueTooth_On_off = 0;
                    }
                }
            }
}

我遇到异常错误:

java.io.exception:[jsr82] connect:未创建连接(失败或中止)

我在嵌入式设备方面也做不了多少。

以下是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.edazzelsmarthome"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.edazzelsmarthome.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".broadcast.PairingRequest">
            <intent-filter> 
                <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 
                <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> 
                </intent-filter>
         </receiver>
    </application>

</manifest>

0 个答案:

没有答案