2014-02-21 30 views
2

所以即时通讯工作的应用程序,直接连接通过蓝牙接受来自RFCOMM通道的字符串命令并发送响应。Android到个人电脑RFCOMM频道

所以这些都是2类我目前正在与

Bluetooth_Manager

import android.bluetooth.*; 
import android.content.Intent; 

public class Reader_BluetoothManager { 

    protected Reader_MainScreen main; 
    protected BluetoothAdapter bAdapter; 
    private Reader_AcceptThread bAccept; 

    public Reader_BluetoothManager(Reader_MainScreen main) { 
     this.main = main; 
     initiate(); 
    } 

    public void initiate(){ 
     checkForBluetooth(); 
     enableBluetooth(); 
    } 

    public void log(String l){ 
     main.log(l); 
    } 

    public void checkForBluetooth(){ 
     bAdapter = BluetoothAdapter.getDefaultAdapter(); 
     if (bAdapter == null) { 
      main.log("No Bluetooth supported!"); 
      return; 
     } 
     main.log("Bluetooth supported..."); 
    } 

    public void enableBluetooth(){ 
     if (!bAdapter.isEnabled()) { 
      main.log("Bluetooth not enabled... requesting activation"); 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      main.startActivityForResult(enableBtIntent, main.BLUETOOTH_ENABLE_CODE); 
     } 
    } 

    public void enableConnection(){ 
     main.log("Connecting..."); 
     if (bAccept != null) 
      bAccept.interrupt(); 
     bAccept = new Reader_AcceptThread(this); 
     bAccept.start(); 
    } 

    public void stopConnection(){ 
     if (bAccept != null) 
      bAccept.interrupt(); 
    } 
} 

而且AcceptThread

import java.io.IOException; 
import java.util.UUID; 

import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothServerSocket; 
import android.bluetooth.BluetoothSocket; 

public class Reader_AcceptThread extends Thread{ 

    private BluetoothServerSocket serverSocket; 
    private BluetoothSocket socket; 
    private BluetoothDevice device; 
    private Reader_BluetoothManager main; 


    public Reader_AcceptThread(Reader_BluetoothManager main) { 
     this.main = main; 
    } 

    public void run(){ 
     try { 
      serverSocket = main.bAdapter.listenUsingRfcommWithServiceRecord("XXXXXX", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
      main.log("checkPoint 1"); 
      socket = serverSocket.accept(); 
      main.log("checkPoint 2"); 
      device = socket.getRemoteDevice(); 
      main.log("checkPoint 3"); 
      main.log("connection established..."); 
      main.log(socket.toString()); 
     } catch (IOException e) { 
      main.log("error..."); 
      main.log(e.toString()); 
      main.main.stopConnection(); 
      interrupt(); 
     } 
     while (!isInterrupted()){ 
      try { 
       sleep(2000); 
      } catch (InterruptedException e) { 
       interrupt(); 
       break; 
      } 
      main.log("ping"); 
     } 
    } 
} 

工作,我这些根据谷歌文档指南构成。 同样在我的笔记本电脑,我正在做以下几点:

enter image description here

,这是,什么是发生在我的电话: enter image description here

这里,发生了什么,当我尝试从连接我的Windows PC: enter image description here

正如你可以看到在手机屏幕截图我卡在socket = serverSocket.accept()

回答

2

您所使用的UUID是该服务的UUID - 如果你想使用默认的RFCOMM通道,你应该使用SPP UUID "00001101-0000-1000-8000-00805F9B34FB"代替UUID.randomUUID();

serverSocket = main.bAdapter.listenUsingRfcommWithServiceRecord("Connection Test", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); 
+0

好感谢你..我不知道我不得不为此使用固定的UUID ..我能够从Windows PC进行连接。我是对的,当我尝试通过'echo TEXT> COM4'发送消息? –

+0

是的,你可以这样做,或使用超级终端并通过它连接。不介意也接受答案:) –

+0

想到另一个问题?..我现在有一个COM端口“COM4”从我的电脑到我的手机,但手机仍然卡在'checkPoint1'上。当我尝试'回显你好> COM4'的cmd说:“系统找不到那个文件”。我也尝试通过腻子连接..但腻子放弃与“无法打开连接到COM4,无法打开串行端口” –