无法通过蓝牙从Android获得Raspberry pi 3的价值

时间:2016-06-26 12:25:09

标签: java android bluetooth raspberry-pi3

我是Java编程的新手,我正试图从' Blueterm'中发送一些价值。 Android上的应用程序,通过蓝牙在我的Raspberry pi 3上接收它。 raspberrypi3已经内置蓝牙,我可以配对2台设备,但我无法连接它们,也不知道如何从Java代码开始。我hava尝试了ConnectedThread和AcceptThread但没有进展,所以如果有人可以帮我这个。



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

import javax.bluetooth.*;
import android.bluetooth.*;

public class AcceptThread extends Thread {
	private final BluetoothServerSocket mmServerSocket;
	private UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    public AcceptThread() {
    	BluetoothAdapter Badp= BluetoothAdapter.getDefaultAdapter();
		  if (Badp == null) {
			    // Device does not support Bluetooth
			  System.out.println("Bluetooth Not Supported");
		  }
        // Use a temporary object that is later assigned to mmServerSocket,
        // because mmServerSocket is final
        BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
            tmp = Badp.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);
        } catch (IOException e) { }
        mmServerSocket = tmp;
    }
 
    public void run() {
        BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        while (true) {
            try {
                socket = mmServerSocket.accept();
            } catch (IOException e) {
                break;
            }
            // If a connection was accepted
            if (socket != null) {
                // Do work to manage the connection (in a separate thread)
                manageConnectedSocket(socket);
                try {
					mmServerSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                break;
            }
        }
    }
 
    /** Will cancel the listening socket, and cause the thread to finish */
    public void cancel() {
        try {
            mmServerSocket.close();
        } catch (IOException e) { }
    }

    private void manageConnectedSocket(BluetoothSocket socket) {
        
        //acceptThread.cancel();
    }
	

}




0 个答案:

没有答案