2012-06-29 28 views
8

के साथ ब्लूकॉव, लैपटॉप और एंड्रॉइड टैबलेट मैं एक साधारण एप्लिकेशन के माध्यम से ब्लूटूथ का उपयोग करने की मूल बातें प्राप्त करने की कोशिश कर रहा हूं। मुझे लैपटॉप एप्लिकेशन भी पसंद आएगा ताकि मैं ब्लूटूथ कॉमर्स को डीबग कर सकूं। नीचे दिया गया कोड लैपटॉप के साथ क्लाइंट (BlueCove 2.1.0 का उपयोग करके) और टैबलेट सर्वर (एंड्रॉइड 2.2) के साथ मेरा प्रयास है।ब्लूटूथ

जो मैं समझता हूं उससे, इसे लिखित रूप में काम करना चाहिए, और लैपटॉप टैबलेट और इसकी पेशकश की गई सेवा दोनों को उठा रहा है। हालांकि, लाइन "StreamConnection conn = (StreamConnection) Connector.open(url, Connector.READ_WRITE);" प्रत्येक बार शून्य वापस आती है।

कोई विचार क्या गलत हो रहा है? पर

BlueCove संस्करण 2.1.0 विनसॉक
पता:: यहाँ कोड से उत्पादन होता है 68A3C44A5265
नाम: WS1497
डिवाइस जांच शुरू ...
डिवाइस की खोज की: 2013E061D922
डिवाइस की खोज की: 00242BFE7375
INQUIRY_COMPLETED
डिवाइस पूछताछ पूर्ण हुई।
सेवा जांच शुरू हुई।
से: Galaxy Tab
सेवा खोज पूरी - कोड: 1
से: WS1190
सेवा खोज पूरी - कोड: 4
ब्लूटूथ डिवाइस:
1. 2013E061D922 (Galaxy Tab)
2. 00242BFE7375 (WS1190)
btspp: // 2013E061D922: 20; प्रमाणित = false; एन्क्रिप्ट = false; मास्टर = false ---- = सूत्र में अशक्त
अपवाद "मुख्य" java.lang.NullPointerException
MainClass.main पर (MainClass। जावा: 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 

एंड्रॉयड:

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(); 
    } 
} 

उत्तर

0

लंबे समय बाद अपडेट का बिट - ब्लूटूथ के लिए यूयूआईडी को "0000xxxx00001000800000805f9b34fb" फॉर्म लेने की आवश्यकता है, जो सवाल पूछता है कि क्यों 128 बिट यूयूआईडी के बजाय 16 बिट पहचानकर्ताओं का उपयोग न करें, लेकिन कभी भी ध्यान न दें।

मुझे नहीं पता कि ब्लूकॉव मेरे लैपटॉप पर इसके साथ काम करता है, लेकिन मेरे लैपटॉप पर लिनक्स और 'ब्लूज़' के साथ मेरे हालिया प्रयोगों का सुझाव है कि उस फॉर्म का कोई यूयूआईडी काम करता है। एंड्रॉइड में शायद कुछ दस्तावेजों के नोट के रूप में उनके दस्तावेज़ों में शामिल होना चाहिए।

0

यह एक पुरानी है सवाल है, इसलिए मुझे नहीं पता कि कोई अभी भी एक उत्तर की तलाश में है, लेकिन फिर भी वैसे भी है। .. :)। जिस लाइन के बारे में आपने पूछा है वह शून्य हो रहा है क्योंकि url शून्य है। अपने कोड में से किसी एक के बजाय इस यूयूआईडी को आजमाएं: 0000110100001000800000805f9b34fb

संबंधित मुद्दे