日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 蓝牙设置平板电脑,java – BlueCove,笔记本电脑和带蓝牙的Android平板电脑

發布時間:2024/1/23 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 蓝牙设置平板电脑,java – BlueCove,笔记本电脑和带蓝牙的Android平板电脑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正試圖通過一個簡單的應用程序獲得使用藍牙的基礎知識.我也喜歡筆記本電腦應用程序,所以我可以簡單地調試藍牙通信.下面的代碼是我嘗試將筆記本電腦作為客戶端(使用BlueCove 2.1.0)而平板電腦作為服務器(

Android 2.2).

根據我的理解,這應該按照書面形式工作,筆記本電腦正在提供平板電腦及其提供的服務.但是,行“StreamConnection conn =(StreamConnection)Connector.open(url,Connector.READ_WRITE);”每次都返回null.

任何想法都出了什么問題?以下是代碼的輸出:

BlueCove version 2.1.0 on winsock

Address: 68A3C44A5265

Name: WS1497

Starting device inquiry…

Device discovered: 2013E061D922

Device discovered: 00242BFE7375

INQUIRY_COMPLETED

Device Inquiry Completed.

Service Inquiry Started.

From: Galaxy Tab

Service search completed – code: 1

From: WS1190

Service search completed – code: 4

Bluetooth Devices:

1. 2013E061D922 (Galaxy Tab)

2. 00242BFE7375 (WS1190)

btspp://2013E061D922:20;authenticate=false;encrypt=false;master=false —-=null

Exception in thread “main” java.lang.NullPointerException

at MainClass.main(MainClass.java:104)

BlueCove stack shutdown completed

這是我正在使用的代碼:

筆記本代碼:

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 vecDevices = new Vector();

private static Vector vecServices = new Vector();

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

}

}

總結

以上是生活随笔為你收集整理的android 蓝牙设置平板电脑,java – BlueCove,笔记本电脑和带蓝牙的Android平板电脑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。