android - 蓝牙错误没有广告设备

标签 android python sockets bluetooth raspberry-pi3

#! /usr/bin/python

import bluetooth
import uuid

server_socket = bluetooth.BluetoothSocket( bluetooth.RFCOMM )

port = 1
server_socket.bind(("",port))
server_socket.listen(1)

uuID = ##generated uuid

bluetooth.advertise_service( server_socket, "test", service_id=uuID )

client_socket, client_address = server_socket.accept()
print(client_socket)
print(client_address)

如果有人可以帮助解决这个问题,那就太好了。我已经尝试按照此处列出的说明进行了大约 5 次:Python code for Bluetooth throws error after I had to reset the adapter

我一直收到错误提示“bluetooth.btcommon.BluetoothError: error no advertisable device” 行号指向 advertise_service 行,无论我是否添加附加参数,它都会这样做,如 pybluez 示例中所示github页面,或者将端口绑定(bind)到 bluetooth.PORT_ANY 被调用的方法在这里:

def advertise_service (sock, name, service_id = "", service_classes = [], \
        profiles = [], provider = "", description = "", protocols = []):
    if service_id != "" and not is_valid_uuid (service_id):
         raise ValueError ("invalid UUID specified for service_id")
    for uuid in service_classes:
        if not is_valid_uuid (uuid):
            raise ValueError ("invalid UUID specified in service_classes")
    for uuid, version in profiles:
        if not is_valid_uuid (uuid) or  version < 0 or  version > 0xFFFF:
            raise ValueError ("Invalid Profile Descriptor")
    for uuid in protocols:
        if not is_valid_uuid (uuid):
            raise ValueError ("invalid UUID specified in protocols")        

    try:
        _bt.sdp_advertise_service (sock._sock, name, service_id, \
                service_classes, profiles, provider, description, \
                protocols)
    except _bt.error as e:
        raise BluetoothError (str (e))

如果我不做广告我无法打印出客户端信息,并且在android端得到一个空指针异常,所以我认为这是必要的,但是如果我做了广告就无法通过这个错误. 这是出现此错误所需的最少代码量。就像我提到的,没有广告结果没有错误,但我无法在连接上打印客户端信息(android 端找不到 pi)。

如果您确实知道在没有该部分的情况下执行此操作的方法,这里是 android 代码:

    Set<BluetoothDevice> pairedDevices = BTAdapter.getBondedDevices();
            TextView textShowConnected = (TextView) findViewById(R.id.textShowConnected);
            if (pairedDevices.size() > 0) 
            {
                for (BluetoothDevice device : pairedDevices) 
                {
                    if(device.getName().toString().equals("Pi"))
                    {
                        textShowConnected.setText("Found the Pi.  Address is "+device.getAddress());
                        TextView textShowConnectedSocket = (TextView) findViewById(R.id.textShowConnectedSocket);
                        //textShowConnectedSocket.setText("uuid is: "+device.getUuids()[0].getUuid().toString());
                        try 
                        {
                            BluetoothSocket connection = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
                            //BluetoothSocket connection = device.createInsecureRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
                            connection.connect();
                            if(connection.isConnected())
                            {
                                textShowConnected.setText("Is connected from second.");
                                textShowConnectedSocket.setText("Is conencted to: "+connection.getRemoteDevice().getName().toString());
                                textShowPlace.setText("Is connected to: "+connection.getRemoteDevice().getAddress().toString());
                            }
                            else
                            {
                                textShowConnected.setText("No connection.");
                                textShowConnectedSocket.setText("No connection.");
                                textShowPlace.setText("No connection.");
                            }
                        } 
                        catch (IOException e) 
                        {
                            e.printStackTrace();
                        }
                    }
                    //DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
                    //deviceItemList.add(newDevice);
                }
            }
        }

我最近的 Java 端尝试(以防 pi 端没问题):

for (BluetoothDevice device : pairedDevices) 
{
    if(device.getName().toString().equals("Pi"))
    {
        textShowConnected.setText("Found the Pi.  Address is "+device.getAddress());
        TextView textShowConnectedSocket = (TextView) findViewById(R.id.textShowConnectedSocket);
        TextView textShowPlace = (TextView) findViewById(R.id.textShowPlace);
        //textShowConnectedSocket.setText("uuid is: "+device.getUuids()[0].getUuid().toString());
        int bt_port_to_connect = 1;
        BluetoothSocket deviceSocket = null;
        Method m = null;
        try {
            textShowPlace.setText("created socket");
            m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
            deviceSocket = (BluetoothSocket) m.invoke(device,bt_port_to_connect);
            deviceSocket.connect();
            if(deviceSocket.isConnected()) 
            {
                textShowPlace.setText("is connected");
                textShowConnectedSocket.setText("Connected successfully.");
                textShowConnected.setText("Connected to: "+deviceSocket.getRemoteDevice().getName().toString());
            }
            else
            {
                textShowConnectedSocket.setText("Did not connect.");
            }
        } 
        catch (IOException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        } 
        catch (NoSuchMethodException e) 
        {
            textShowConnected.setText("No such method.");
            textShowPlace.setText("catch statement "+e);
        } 
        catch (InvocationTargetException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        } 
        catch (IllegalAccessException e) 
        {
            textShowPlace.setText("catch statement "+e);
            textShowConnectedSocket.setText("No connection.");
        }
        //device.createRfcommSocketToServiceRecord(uuid);                                                                            
        //device.createInsecureRfcommSocketToServiceRecord(uuid);
    }
    //DeviceItem newDevice = new DeviceItem(device.getName(), device.getAddress(), "false");
    //deviceItemList.add(newDevice);
}

对于建立连接方面的任何帮助,我将不胜感激。我不确定这里有什么问题。

最佳答案

看起来只是需要做广告:“sudo hciconfig hci0 piscan”

并且要连接它需要来自该导入的 bluetooth.PORT_ANY,所以我将其标记为已回答。

对于任何因为自己的问题而发现这一点的人。你现在有了答案。祝你好运。

关于android - 蓝牙错误没有广告设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37913796/

相关文章:

python - 用 Python 编写模式字母表程序

c - TCP监听并在监听的同时做其他事情

java - 从 SSLSocket 读取最快或最好的方法

c++ - 通过从 C++ 到 nodejs 进程的套接字连接发送 250ms UDP 价格滴答,为什么?

android - 你如何将 onDestroy 放在 recyclerview 适配器中?是否可以?

android - 在不强制用户登录/允许的情况下嵌入公共(public) Facebook 页面的 Activity 提要

java - 将 DAO 注入(inject)构造函数是否被认为是不好的做法?如果是这样,为什么?

Python 随机音频

javascript - 在 React Native 中如何获取带认证的网络图片?

python - 使用 Python 按顺序选择行