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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接

發布時間:2024/10/8 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我已經嘗試了其他評論中的所有建議,但都沒有效果,我希望有人能幫助我。我已經為這個問題掙扎了三天了。我確信我的uuid是正確的,并且我知道清單中啟用了藍牙訪問。

我正在嘗試將我的android應用程序連接到運行在Fedora中的python服務器。它斷斷續續地工作,現在一點也不起作用。我收到的android異常通常是。。當在下面附加的代碼中執行btSocket.connect()時,將拋出這些值。12-09 05:08:42.331: ERROR/BluetoothService(676): java.io.IOException: Service discovery failed

或者12-09 05:27:00.757: ERROR/BluetoothService(729): java.io.IOException: Service discovery failed

這是我的android藍牙課程,應該負責一切。當主應用程序類收到套接字已連接到的消息時,線程將啟動。我的藍牙類基于http://www.anddev.org/viewtopic.php?p=35487#35487。package spin.halo;

import java.io.*;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.UUID;

import android.bluetooth.*;

import android.os.Handler;

import android.util.Log;

public class BluetoothService extends Thread{

private static final String TAG = "BluetoothService";

private static final boolean D = true;

private BluetoothAdapter mBluetoothAdapter = null;

private BluetoothSocket btSocket = null;

private OutputStream outStream = null;

private InputStream inStream = null;

private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

private static String address;

private Handler appHandler;

public BluetoothService(Handler h) {

if (D)

Log.e(TAG, "+++ ON CREATE +++");

appHandler = h;

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

if (mBluetoothAdapter == null) {

Log.e(TAG, "NO BT ADAPTER!");

return;

}

if (!mBluetoothAdapter.isEnabled()) {

Log.e(TAG, "Bluetooth is not enabled!");

return;

}

if (D)

Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");

}

public void connectToServer() {

connectToServer("60:33:4B:25:0D:37");

}

public void connectToServer(String serverMacAddress) {

address = serverMacAddress;

//

if (D) {

Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");

}

BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

Log.v(TAG, "REMOTE DEVICE: " + device.toString());

try {

btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

Log.v(TAG, "SOCKET: " + btSocket.toString());

} catch (Exception e) {

Log.e(TAG, "ON RESUME: Socket creation failed.", e);

}

/* Discovery may be going on, e.g., if you're running a

'scan for devices' search from your handset's Bluetooth

settings, so we call cancelDiscovery(). It doesn't hurt

to call it, but it might hurt not to... discovery is a

heavyweight process; you don't want it in progress when

a connection attempt is made.*/

mBluetoothAdapter.cancelDiscovery();

// Blocking connect, for a simple client nothing else can

// happen until a successful connection is made, so we

// don't care if it blocks.

try {

btSocket.connect();

Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");

appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_CONNECTION_MADE, ""));

} catch (IOException e) {

try {

Log.e(TAG, "ON RESUME: Could not connect", e);

btSocket.close();

} catch (IOException e2) {

Log.e(TAG, "ON RESUME: Unable to close socket during connection failure", e2);

}

}

// Create output stream

try {

outStream = btSocket.getOutputStream();

} catch (IOException e) {

Log.e(TAG, "ON RESUME: Output stream creation failed.", e);

}

// Create input stream

try {

inStream = btSocket.getInputStream();

} catch (IOException e) {

Log.e(TAG, "Input stream creation failed.", e);

}

}

public void write(String message) {

if(message.length() > 0) {

byte[] msgBuffer = message.getBytes();

try {

outStream.write(msgBuffer);

} catch (IOException e) {

Log.e(TAG, "ON RESUME: Exception during write.", e);

}

}

}

public void run() {

LineNumberReader mLineReader = new LineNumberReader(new InputStreamReader(inStream));

while(true) {

try {

String message = mLineReader.readLine();

if(D) {Log.v(TAG, "Bluetooth says: " + message);}

Log.v(TAG, appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message).toString());

appHandler.sendMessage(appHandler.obtainMessage(ValidationApp.BT_MESSAGE, message));

} catch (IOException e) {

Log.e(TAG, "startListen: ", e);

}

}

}

}

下面是我的python代碼的關鍵部分。我對這個密碼很有信心。# pybluez library

import bluetooth

server_socket = bluetooth.BluetoothSocket( bluetooth.RFCOMM )

client_sockets = []

server_socket.bind(("",bluetooth.PORT_ANY))

port = server_socket.getsockname()[1]

uuid = "00001101-0000-1000-8000-00805F9B34FB"

print "Listening for devices..."

# advertise service

server_socket.listen(1)

bluetooth.advertise_service( server_socket, "Validation Host",

service_id = uuid,

service_classes = [ uuid, bluetooth.SERIAL_PORT_CLASS ],

profiles = [ bluetooth.SERIAL_PORT_PROFILE ],

)

# accept incoming connections

client_sock, client_info = server_socket.accept()

client_sockets.append(client_sock)

print "Accepted Connection from ", client_info

謝謝你看我。

總結

以上是生活随笔為你收集整理的Android蓝牙无法通信,android.bluetooth.BluetoothSocket无法连接的全部內容,希望文章能夠幫你解決所遇到的問題。

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