2013-07-26 196 views
0

我已经做了一个简单的应用程序,通过BT连接我的手机与Arduino,一切顺利。我的手机有Android 2.3.6 ...但是,当我尝试我的平板电脑上的应用程序(与Android 4.0.3),我无法连接。我在这里发布特定的一段代码:在Android中连接蓝牙

try { 
     BluetoothSocket socket = mydevice.createRfcommSocketToServiceRecord(UUID.fromString(ARDUINO_STANDAR_UUID)); 
     socket.connect(); 
     OutputStream output = socket.getOutputStream(); 
     InputStream input = socket.getInputStream(); 
     Log.d(TAG, "Connected"); 
    } 
    catch (IOException e) { Log.e(TAG, e.getMessage()); } 
} 

使用日志,问题在socket.connect();在这一点上,我必须输入设备的PIN码,但它不会连接...错误是“连接被拒绝”...

什么可能是错误的?

回答

0

试试类似这样的照顾连接。我刚刚更新了一个旧的android-10应用程序到android-17,并处理完全这一点。

BluetoothDevice yourDevice; 
try { 
     Method m = yourDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class }); 
     selectedDeviceSocket = (BluetoothSocket) m.invoke(yourDevice, Integer.valueOf(1)); 
     selectedDeviceSocket.connect(); 
    } 
+0

它的首选设备? – user2624284

+0

我不知道女巫的对象必须是... – user2624284

+0

对不起,这只是你的设备。我出于某种原因改变了名字。 –