Java BlueCove、笔记本电脑和带蓝牙的Android平板电脑

Java BlueCove、笔记本电脑和带蓝牙的Android平板电脑,java,android,bluetooth,bluecove,Java,Android,Bluetooth,Bluecove,我正试图通过一个简单的应用程序获得使用蓝牙的基础知识。我还想要一个笔记本电脑应用程序,这样我就可以简单地调试蓝牙通信。下面的代码是我尝试将笔记本电脑作为客户端(使用BlueCove 2.1.0),平板电脑作为服务器(Android 2.2) 据我所知,这应该是书面的,笔记本电脑正在接收平板电脑及其提供的服务。但是,行“StreamConnection conn=(StreamConnection)Connector.open(url,Connector.READ_WRITE);”每次都返回nul

我正试图通过一个简单的应用程序获得使用蓝牙的基础知识。我还想要一个笔记本电脑应用程序,这样我就可以简单地调试蓝牙通信。下面的代码是我尝试将笔记本电脑作为客户端(使用BlueCove 2.1.0),平板电脑作为服务器(Android 2.2)

据我所知,这应该是书面的,笔记本电脑正在接收平板电脑及其提供的服务。但是,行
“StreamConnection conn=(StreamConnection)Connector.open(url,Connector.READ_WRITE);”
每次都返回null

知道怎么回事吗?以下是代码的输出:

winsock上的BlueCove 2.1.0版
地址:68A3C44A5265
名称:WS1497
正在启动设备查询…
发现的设备:2013E061D922
发现的设备:00242BFE7375
查询已完成
设备查询已完成
服务查询已启动
发件人:Galaxy选项卡
服务搜索已完成-代码:1
发件人:WS1190
服务搜索已完成-代码:4
蓝牙设备:
1.2013E061D922(银河系选项卡)
2.00242BFE7375(WS1190)
btspp://2013E061D922:20;验证=假;加密=假;master=false---=null
线程“main”java.lang.NullPointerException中出现异常
在MainClass.main(MainClass.java:104)
BlueCove堆栈关闭已完成

下面是我使用的代码:

笔记本电脑代码:

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;

public class MainClass implements DiscoveryListener {

// object used for waiting
private static Object lock = new Object();

// vector containing the devices discovered
private static Vector<RemoteDevice> vecDevices = new Vector<RemoteDevice>();
private static Vector<String> vecServices = new Vector<String>();

// main method of the application
public static void main(String[] args) throws IOException {

    // create an instance of this class
    MainClass bluetoothDeviceDiscovery = new MainClass();

    // display local device address and name
    LocalDevice localDevice = LocalDevice.getLocalDevice();

    System.out.println("Address: " + localDevice.getBluetoothAddress());
    System.out.println("Name: " + localDevice.getFriendlyName());

    // find devices
    DiscoveryAgent agent = localDevice.getDiscoveryAgent();

    System.out.println("Starting device inquiry...");
    agent.startInquiry(DiscoveryAgent.GIAC, bluetoothDeviceDiscovery);

    try {
        synchronized (lock) {
            lock.wait();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println("Device Inquiry Completed. ");
    System.out.println("Service Inquiry Started. ");

    UUID uuids[] = new UUID[1];
    uuids[0] = new UUID("fa87c0d0afac11de8a390800200c9a66", false);

    for (RemoteDevice rd : vecDevices) {
        System.out.println("From: " + rd.getFriendlyName(false));
        agent.searchServices(null, uuids, rd, bluetoothDeviceDiscovery);
        try {
            synchronized (lock) {
                lock.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    // print all devices in vecDevices
    int deviceCount = vecDevices.size();

    if (deviceCount <= 0) {
        System.out.println("No Devices Found .");
    } else {
        // print bluetooth device addresses and names in the format [ No.
        // address (name) ]
        System.out.println("Bluetooth Devices: ");
        for (int i = 0; i < deviceCount; i++) {
            RemoteDevice remoteDevice = (RemoteDevice) vecDevices
                    .elementAt(i);
            System.out.println((i + 1) + ". "
                    + remoteDevice.getBluetoothAddress() + " ("
                    + remoteDevice.getFriendlyName(false) + ")");
        }
    }

    // System.out.println("SR: " + sr.toString());
    for (String url : vecServices) {
        try {
            String url = sr
                    .getConnectionURL(
                            ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
            StreamConnection conn = (StreamConnection) Connector.open(url, Connector.READ_WRITE);
            System.out.println(url + " ----=" + conn);
            DataInputStream din = new DataInputStream(
                    conn.openDataInputStream());
            synchronized (lock) {
                try {
                    lock.wait(10);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            while (din.available() != 0) {
                System.out.print(din.readChar());
            }
            System.out.println();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}// end main

// methods of DiscoveryListener

/**
 * This call back method will be called for each discovered bluetooth
 * devices.
 */
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    System.out.println("Device discovered: "
            + btDevice.getBluetoothAddress());
    // add the device to the vector
    if (!vecDevices.contains(btDevice)) {
        vecDevices.addElement(btDevice);
    }
}

// no need to implement this method since services are not being discovered
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    for (ServiceRecord sr : servRecord) {
        vecServices.add(sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false));
    }
}

// no need to implement this method since services are not being discovered
public void serviceSearchCompleted(int transID, int respCode) {
    System.out.println("Service search completed - code: " + respCode);
    synchronized (lock) {
        lock.notify();
    }
}

/**
 * This callback method will be called when the device discovery is
 * completed.
 */
public void inquiryCompleted(int discType) {
    switch (discType) {
    case DiscoveryListener.INQUIRY_COMPLETED:
        System.out.println("INQUIRY_COMPLETED");
        break;

    case DiscoveryListener.INQUIRY_TERMINATED:
        System.out.println("INQUIRY_TERMINATED");
        break;

    case DiscoveryListener.INQUIRY_ERROR:
        System.out.println("INQUIRY_ERROR");
        break;

    default:
        System.out.println("Unknown Response Code");
        break;
    }
    synchronized (lock) {
        lock.notify();
    }
}// end method
}// end class

这是一个老问题,所以我不知道是否还有人在寻找答案,但这里有一个答案…:)。您询问的行返回null,因为
url
为null。尝试使用此UUID而不是代码中的UUID:
0000110100080000805F9B34FB

经过很长时间的更新后,蓝牙要求UUID采用“00003xx00001080000805F9B34FB”的形式,这就提出了一个问题:为什么不使用16位标识符而不是128位UUID,但没关系

我不知道BlueCove是否能在我的笔记本电脑上使用它,但我最近在我的笔记本电脑上用Linux和“Bluez”进行的实验表明,任何这种形式的UUID都能工作。安卓或许应该在他们的文档中包含这一点,作为某种注释

package com.mira.Bluetooth;

import java.io.IOException;

import java.util.UUID;

import android.app.Activity;

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

import android.os.Bundle;

import android.util.Log;

public class BluetoothAndroidActivity extends Activity implements Runnable {
    BluetoothServerSocket bss;
    Thread t;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();

        for (BluetoothDevice btd : bta.getBondedDevices()) {
            Log.i("Bluetooth Device Found",
                  btd.toString() + "; " + btd.getName());
        }

        try {
            bss =
bta.listenUsingRfcommWithServiceRecord("BluetoothChat", UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
            t = new Thread(this);
            t.start();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        boolean bContinue = true;
        while (bContinue) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {

            }

            try {
                System.out.println("Listening for connection");
                BluetoothSocket bs = bss.accept();
                System.out.println("Connection received");
                bs.getOutputStream().write("Hello BlueTooth World".getBytes());
                bs.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                bContinue = false;
            }
        }
    }

    /*
 * (non-Javadoc)
 *
 * @see android.app.Activity#onDestroy()
 */

    @Override
    protected void onStop() {
        try {
            System.out.println("Killing ServerSocket");
            bss.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        super.onStop();
    }
}