为什么我无法通过蓝牙将数据从我的Matlab应用程序(PC)发送到我的Android平板电脑?

时间:2016-05-03 09:02:18

标签: java android matlab bluetooth

顺便说一下:我英语不流利,温柔, 我试图通过蓝牙在我的Matlab应用程序中将数据发送到我的Android应用程序。

我开始在我的Android应用程序上创建服务器蓝牙:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* Created by Ytysen on 28/04/2016.
 */

public class BluetoothReceiver extends Thread {
private BluetoothServerSocket mmServerSocket;
private BluetoothAdapter bluetoothAdapter;
private AtomicBoolean stop;
private BluetoothListener bluetoothListener;

public BluetoothReceiver(BluetoothDevice device, BluetoothAdapter bluetoothAdapter) {
    BluetoothServerSocket tmp = null;
    this.bluetoothAdapter = bluetoothAdapter;
    this.stop = new AtomicBoolean(false);
    mmDevice = device;
    try {
        tmp = bluetoothAdapter.listenUsingRfcommWithServiceRecord("ReconnaissanceFaciale", UUID.randomUUID());
    } catch (IOException e) {

    }
    mmServerSocket = tmp;
}

public void setBluetoothReceiver(BluetoothListener bluetoothListener) {
    this.bluetoothListener = bluetoothListener;
}

public void stopCom() {
    this.stop.set(true);
}

@Override
public void run() {
    BluetoothSocket socket = null;
    bluetoothAdapter.cancelDiscovery();
    while (true) {
        try {
            socket = mmServerSocket.accept();
            Log.d("Message","Passage 2");
        } catch (IOException e) {
            Log.d("Message","No Passage 2");
            break;
        }
        if (socket != null) {
            manageConnectedSocket(socket);
            try {
                mmServerSocket.close();
                Log.d("Message","Passage 3");
            } catch (IOException e) {
                Log.d("Message","No Passage 3");
                e.printStackTrace();
            }
            break;
        }

    }
}

private void manageConnectedSocket(BluetoothSocket socket) {
    String msg = socket.toString();
    Log.d("Message","Passage 4");
    bluetoothListener.onMessageReceived(msg);
    try {
        sleep(30);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void cancel() {
    try {
        mmServerSocket.close();
    } catch (IOException e) { }
}

public interface BluetoothListener {
    public void onMessageReceived(String message);
}
}

我正在Matlab上发送数据:

b = Bluetooth('LUC', 5);
fopen(b);
fprintf(b,'%s','Do you want to build a snowman ?');
fclose(b);

我可以在btsnoop_hci.log中看到我的消息: btsnoop_hci.log image

enter image description here

我的Android应用程序永远不会通过我的尝试在void run()... 有什么想法吗?

在我的" MyActivity"中,我做到了:

    if (mBluetoothAdapter == null)
     { 
Toast.makeText(getApplicationContext(), "Pas de Bluetooth", Toast.LENGTH_SHORT).show();// Device does not support Bluetooth 
} 
if (!mBluetoothAdapter.isEnabled()) 
{ 
mBluetoothAdapter.enable(); 
} BluetoothReceiver bluetoothReceiver = new BluetoothReceiver(device, mBluetoothAdapter); 
bluetoothReceiver.setBluetoothReceiver(this); bluetoothReceiver.start();

0 个答案:

没有答案