Java me 如何使用j2me连接到bluetoothbee设备?

Java me 如何使用j2me连接到bluetoothbee设备?,java-me,bluetooth,midp,jsr82,Java Me,Bluetooth,Midp,Jsr82,我在j2me中开发了一个简单的蓝牙连接应用程序 我在emulator上试用了它,服务器和客户端都可以找到对方,但当我将应用程序部署到blackberry手机并连接到bluetoothbee设备时,它会显示服务搜索无记录 有什么可能是错的?是j2me在bluetoothbee中找不到服务吗 j2me本身成功地找到了bluetoothbee设备,但为什么它找不到服务呢 我的代码如下。我不明白的是UUID?如何为未知源设置UUID?因为我不知道bluetoothbee设备的UUID class Sea

我在j2me中开发了一个简单的蓝牙连接应用程序

我在emulator上试用了它,服务器和客户端都可以找到对方,但当我将应用程序部署到blackberry手机并连接到bluetoothbee设备时,它会显示
服务搜索无记录

有什么可能是错的?是j2me在bluetoothbee中找不到服务吗

j2me本身成功地找到了bluetoothbee设备,但为什么它找不到服务呢

我的代码如下。我不明白的是UUID?如何为未知源设置UUID?因为我不知道bluetoothbee设备的UUID

class SearchingDevice extends Canvas implements Runnable,CommandListener,DiscoveryListener{

//......

public SearchingDevice(MenuUtama midlet, Display display){
    this.display = display;
    this.midlet = midlet;
    t = new Thread(this);
    t.start();

    timer = new Timer();
    task = new TestTimerTask();

    /*--------------------Device List------------------------------*/
        select = new Command("Pilih",Command.OK,0);
        back = new Command("Kembali",Command.BACK,0);
        btDevice = new List("Pilih Device",Choice.IMPLICIT);
        btDevice.addCommand(select);
        btDevice.addCommand(back);
        btDevice.setCommandListener(this);

    /*------------------Input Form---------------------------------*/

        formInput = new Form("Form Input");
        nama = new TextField("Nama","",50,TextField.ANY);
        umur = new TextField("Umur","",50,TextField.ANY);
        measure = new Command("Ukur",Command.SCREEN,0);
        gender = new ChoiceGroup("Jenis Kelamin",Choice.EXCLUSIVE);
        formInput.addCommand(back);
        formInput.addCommand(measure);
        gender.append("Pria", null);
        gender.append("Wanita", null);
        formInput.append(nama);
        formInput.append(umur);
        formInput.append(gender);

        formInput.setCommandListener(this);
     /*---------------------------------------------------------------*/


        findDevice();
}


/*----------------Gambar screen searching device---------------------------------*/
protected void paint(Graphics g) {

        g.setColor(0,0,0);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(255,255,255);
        g.drawString("Mencari Device", 20, 20, Graphics.TOP|Graphics.LEFT);

    if(this.counter == 1){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
    }
    if(this.counter == 2){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
    }
    if(this.counter == 3){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
        g.setColor(255,115,200);
        g.fillRect(100, 60, 20, 60);
    }
    if(this.counter == 4){
        g.setColor(255,115,200);
        g.fillRect(20, 100, 20, 20);
        g.setColor(100,255,255);
        g.fillRect(60, 80, 20, 40);
        g.setColor(255,115,200);
        g.fillRect(100, 60, 20, 60);
        g.setColor(100,255,255);
        g.fillRect(140, 40, 20, 80);
        //display.callSerially(this);
    }


}
/*--------- Running Searching Screen ----------------------------------------------*/
public void run() {
    while(run){

       this.counter++;
       if(counter > 4){
           this.counter = 1;
       }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            System.out.println("interrupt"+ex.getMessage());
        }
         repaint();
    }
}

/*-----------------------------cari device bluetooth yang -------------------*/
public void findDevice(){
    try {
        devices = new java.util.Vector();
        local = LocalDevice.getLocalDevice();
        agent = local.getDiscoveryAgent();
        local.setDiscoverable(DiscoveryAgent.GIAC);
        agent.startInquiry(DiscoveryAgent.GIAC, this);
    } catch (BluetoothStateException ex) {
        System.out.println("find device"+ex.getMessage());
    }
}
/*-----------------------------jika device ditemukan--------------------------*/
public void deviceDiscovered(RemoteDevice rd, DeviceClass dc) {
    devices.addElement(rd);
}
/*--------------Selesai tes koneksi ke bluetooth server--------------------------*/
public void inquiryCompleted(int param) {
    switch(param){
        case DiscoveryListener.INQUIRY_COMPLETED: //inquiry completed normally
            if(devices.size()>0){ //at least one device has been found
                services = new java.util.Vector();
                this.findServices((RemoteDevice)devices.elementAt(0));
                this.run = false;
                do_alert("Inquiry completed",4000);
            }else{
                do_alert("No device found in range",4000);
            }
            break;
        case DiscoveryListener.INQUIRY_ERROR:
            do_alert("Inquiry error",4000);
            break;
        case DiscoveryListener.INQUIRY_TERMINATED:
            do_alert("Inquiry canceled",4000);
            break;
    }
}
/*-------------------------------Cari service bluetooth server----------------------------*/
public void findServices(RemoteDevice device){
     try {
        // int[] attributes = {0x100,0x101,0x102};
        UUID[] uuids = new UUID[1];
        //alamat server
       uuids[0] = new UUID("F0E0D0C0B0A000908070605040302010",false);
        //uuids[0] = new UUID("8841",true);
        //menyiapkan device lokal
        local = LocalDevice.getLocalDevice();
        agent = local.getDiscoveryAgent();
        //mencari service dari server

        agent.searchServices(null, uuids, device, this);
        //server = (StreamConnectionNotifies)Connector.open(url.toString());

    } catch (BluetoothStateException ex) {
       // ex.printStackTrace();
        System.out.println("Errorx"+ex.getMessage());
    }
}
/*---------------------------Pencarian service selesai------------------------*/
public void serviceSearchCompleted(int transID, int respCode) {
    switch(respCode){
       case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
           if(currentDevice == devices.size() - 1){
               if(services.size() > 0){
                   this.run = false;
                   display.setCurrent(btDevice);
                   do_alert("Service found",4000);
               }else{
                   do_alert("The service was not found",4000);
               }
           }else{
               currentDevice++;
               this.findServices((RemoteDevice)devices.elementAt(currentDevice));
           }
           break;
       case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
           do_alert("Device not Reachable",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_ERROR:
           do_alert("Service search error",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
           do_alert("No records return",4000);
           break;
       case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
           do_alert("Inquiry canceled",4000);
           break;
   }    }

public void servicesDiscovered(int i, ServiceRecord[] srs) {
    for(int x=0; x<srs.length;x++)
         services.addElement(srs[x]);
    try {
            btDevice.append(((RemoteDevice)devices.elementAt(currentDevice)).getFriendlyName(false),null);
    } catch (IOException ex) {
        System.out.println("service discover"+ex.getMessage());
    }
}
    public void do_alert(String msg, int time_out){
    if(display.getCurrent() instanceof Alert){
        ((Alert)display.getCurrent()).setString(msg);
        ((Alert)display.getCurrent()).setTimeout(time_out);
    }else{
        Alert alert = new Alert("Bluetooth");
        alert.setString(msg);
        alert.setTimeout(time_out);
        display.setCurrent(alert);
    }
    }

private String getData(){
    System.out.println("getData");
    String cmd="";

     try {
            ServiceRecord service = (ServiceRecord)services.elementAt(btDevice.getSelectedIndex());
            String url = service.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
            conn = (StreamConnection)Connector.open(url);
            DataInputStream in = conn.openDataInputStream();
            int i=0;

            timer.schedule(task, 15000);
            char c1;
               while(time){

                 //while(((c1 = in.readChar())>0) && (c1 != '\n')){
                //while(((c1 = in.readChar())>0) ){
                    c1 = in.readChar();
                cmd = cmd + c1;
                //System.out.println(c1);
               // }
               }
                 System.out.print("cmd"+cmd);


                if(time == false){
                in.close();
                conn.close();
                }

        } catch (IOException ex) {
            System.err.println("Cant read data"+ex);
        }
     return cmd;
}




//timer task fungsinya ketika telah mencapai waktu yg dijadwalkan putus koneksi
private static class TestTimerTask extends TimerTask{

    public TestTimerTask() {
    }
    public void run() {

        time = false;
    }
}   
}
class SearchingDevice extends Canvas实现可运行、CommandListener、DiscoveryListener{
//......
公共搜索设备(MenuUtama midlet,显示器){
this.display=显示;
this.midlet=midlet;
t=新螺纹(本螺纹);
t、 start();
定时器=新定时器();
任务=新的TestTimerTask();
/*--------------------设备列表------------------------------*/
选择=新命令(“Pilih”,Command.OK,0);
back=新命令(“Kembali”,Command.back,0);
btDevice=新列表(“Pilih Device”,Choice.IMPLICIT);
btDevice.addCommand(选择);
btDevice.addCommand(返回);
btDevice.setCommandListener(此);
/*------------------输入表格---------------------------------*/
formInput=新表格(“表格输入”);
nama=新文本字段(“nama”,50,TextField.ANY);
umur=新文本字段(“umur”,“”,50,TextField.ANY);
测量=新命令(“Ukur”,命令屏幕,0);
性别=新ChoiceGroup(“Jenis Kelamin”,Choice.EXCLUSIVE);
formInput.addCommand(返回);
formInput.addCommand(测量);
性别。追加(“Pria”,空);
性别。追加(“Wanita”,空);
格式输入附加(nama);
formInput.append(umur);
formInput.append(性别);
formInput.setCommandListener(此);
/*---------------------------------------------------------------*/
findDevice();
}
/*----------------Gambar屏幕搜索装置---------------------------------*/
受保护的空心漆(图g){
g、 setColor(0,0,0);
g、 fillRect(0,0,getWidth(),getHeight());
g、 setColor(255255);
g、 拉丝(“门卡里装置”,20,20,图形。上|图形。左);
if(this.counter==1){
g、 setColor(255115200);
g、 fillRect(20,100,20,20);
}
if(this.counter==2){
g、 setColor(255115200);
g、 fillRect(20,100,20,20);
g、 setColor(100255255);
g、 fillRect(60,80,20,40);
}
if(this.counter==3){
g、 setColor(255115200);
g、 fillRect(20,100,20,20);
g、 setColor(100255255);
g、 fillRect(60,80,20,40);
g、 setColor(255115200);
g、 fillRect(100,60,20,60);
}
if(this.counter==4){
g、 setColor(255115200);
g、 fillRect(20,100,20,20);
g、 setColor(100255255);
g、 fillRect(60,80,20,40);
g、 setColor(255115200);
g、 fillRect(100,60,20,60);
g、 setColor(100255255);
g、 fillRect(140,40,20,80);
//display.callSerially(this);
}
}
/*---------运行搜索屏幕----------------------------------------------*/
公开募捐{
while(运行){
这个.counter++;
如果(计数器>4){
这个计数器=1;
}
试一试{
睡眠(1000);
}捕获(中断异常例外){
System.out.println(“中断”+ex.getMessage());
}
重新油漆();
}
}
/*-----------------------------蓝牙设备-------------------*/
公共无效查找设备(){
试一试{
devices=new java.util.Vector();
local=LocalDevice.getLocalDevice();
agent=local.getDiscoveryAgent();
local.setDiscoverable(DiscoveryAgent.GIAC);
startInquiry代理(DiscoveryAgent.GIAC,this);
}捕获(BluetoothStateException-ex){
System.out.println(“查找设备”+ex.getMessage());
}
}
/*-----------------------------吉卡木坎--------------------------*/
发现公共无效设备(远程设备rd、设备类dc){
装置.补充件(rd);
}
/*--------------Selesai tes koneksi ke蓝牙服务器--------------------------*/
公共无效查询已完成(int参数){
开关(参数){
case DiscoveryListener.INQUIRY\u COMPLETED://查询正常完成
如果(devices.size()>0){//至少找到一个设备
services=newjava.util.Vector();
this.findServices((RemoteDevice)devices.elementAt(0));
this.run=false;
do_警报(“查询完成”,4000);
}否则{
do_警报(“范围内未找到设备”,4000);
}
打破
案例发现Listener.INQUIRY\u错误:
do_警报(“查询错误”,4000);
打破
案例发现Stenner.INQUIRY\u终止:
do_警报(“查询已取消”,4000);
打破
}
}
/*-------------------------------Cari服务蓝牙服务器----------------------------*/
公共void findServices(远程设备){
试一试{
//int[]属性={0x100,0x101,0x102};
UUID[]uuids=新的UUID[1];
//阿拉马特服务器
UUID[0]=新的UUID(“F0E0D0C0B0A00090807060504030010”,false);
//UUID[0]=新的UUID(“8841”,真);
//menyiapkan设备lokal
local=LocalDevice.getLocalDevice();
agent=local.getDiscoveryAgent();
//mencari服务dari服务器
searchServices(null、uuids、device、this);
//server=(StreamConnectionNotifies)Connector.open(url.toString());
}捕获(BluetoothStateException-ex){
//例如printStackTrace();
System.out.println(“Errorx”+ex.getMessage());
}
}
/*---------------------------P