2016-11-02 11 views
0

AndroidとIOT(Intel Galileo)デバイス間でBluetooth通信を確立しようとしています。Android通信デバイスとIOTデバイス

IOT側のコード(私はクライアントとして保管しています)は、アンドロイドにデータを送信しますが、ここでは1つのポート番号はハードコードされています。これはPythonで書かれています。

def record_transmit_to_subscriber(self, subscriber, message): 
    server_addr = subscriber 
    port = 6 
    client_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM) 
    try: 
     client_socket.connect((server_addr, port)) 

     client_socket.send(message) 

     client_socket.close() 
     return True 
    except Exception as e: 
     print "Unable to make connection with subscriber", subscriber 
     return False 

今アンドロイド(サーバ)側で:

private static UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
try { 
     BluetoothServerSocket tmp = null; 
     BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     try { 
      // MY_UUID is the applications UUID string, also used by the client code 
      tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID); 
     } catch (IOException e) { 
      GlobalUtils.writeLogFile("Error in BLE Listening " + e.getMessage()); 
     } 
     mmServerSocket = tmp; 
    } catch (Exception e){ 
     GlobalUtils.writeLogFile("Exception in Accept Thread " + e.getMessage()); 
    } 

私は、サーバー側で、それはUUIDを使用している間に、クライアント側で、それは、ポート番号を使用している、このコードでは、いくつかの問題であっ信じています。誰かがこのコードを変更して接続作業を行う方法を修正してください。

答えて

0

はあなたがソケットとストリームを作成するのを忘れているようだ:

try { 
    BluetoothDevice bluetoothDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice("<MAC_address_of_your_device>"); 
    mSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); 

    mSocket.connect(); 
} catch (IOException e) { 
    Log.e(TAG, "Fail connect"); 
} 

// Get the input and output streams for BT socket 
try { 
    inStream = mSocket.getInputStream(); 
    outStream = mSocket.getOutputStream(); 
} catch (IOException e) { 
    Log.e(TAG, "Fail to open socket streams"); 
} 

あなたは)たとえばoutStream.write(<data_buffer>); outStream.flush();のために(outStreamに例inStream.read(<data_buffer>);と書き込みデータのためのThreadのために(inStreamからの読み出しデータのためのThreadを作成することができるよりも

+0

。 rfcomm socketは自分のアンドロイドコードですでに作成されています –

+0

あなたのアプリケーションに '

関連する問題