对于 Android Nexus 5,调用 getBluetoothService() 时无需使用 BluetoothManagerCallback

2024-04-16

我将实现一个模块,用于通过蓝牙从 Android 智能手机向 HC-06 发送命令。当执行时,出现以下异常,并且找不到标题所示的错误消息的线索。请问修改方法可以吗?

异常日志消息:

 07-29 13:51:37.701: W/BluetoothAdapter(1928): getBluetoothService() called with no BluetoothManagerCallback
    07-29 13:51:37.711: D/BluetoothSocket(1928): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[51]}
    07-29 13:51:42.831: W/System.err(1928): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
    07-29 13:51:42.831: W/System.err(1928):     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
    07-29 13:51:42.831: W/System.err(1928):     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
    07-29 13:51:42.831: W/System.err(1928):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
    07-29 13:51:42.831: W/System.err(1928):     at com.luugiathuy.apps.remotebluetooth.BluetoothCommandService$ConnectThread.run(BluetoothCommandService.java:260)
    07-29 13:51:42.831: D/BluetoothCommandService(1928): setState() 2 -> 1

下面是我的代码

public class BluetoothCommandService {
    // Debugging
    private static final String TAG = "BluetoothCommandService";
    private static final boolean D = true;

    // Unique UUID for this application
   // private static final UUID MY_UUID = UUID.fromString("04c6093b-0000-1000-8000-00805f9b34fb");

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

    // Member fields
    private final BluetoothAdapter mAdapter;
    private final Handler mHandler;
    private ConnectThread mConnectThread;
    private ConnectedThread mConnectedThread;
    private int mState;
//    private BluetoothDevice mSavedDevice;
//    private int mConnectionLostCount;

    // Constants that indicate the current connection state
    public static final int STATE_NONE = 0;       // we're doing nothing
    public static final int STATE_LISTEN = 1;     // now listening for incoming connections
    public static final int STATE_CONNECTING = 2; // now initiating an outgoing connection
    public static final int STATE_CONNECTED = 3;  // now connected to a remote device

    // Constants that indicate command to computer
    public static final int EXIT_CMD = -1;
    public static final int VOL_UP = 1;
    public static final int VOL_DOWN = 2;
    public static final int MOUSE_MOVE = 3;

    /**
     * Constructor. Prepares a new BluetoothChat session.
     * @param context  The UI Activity Context
     * @param handler  A Handler to send messages back to the UI Activity
     */
    public BluetoothCommandService(Context context, Handler handler) {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mState = STATE_NONE;
        //mConnectionLostCount = 0;
        mHandler = handler;
    }

    /**
     * Set the current state of the chat connection
     * @param state  An integer defining the current connection state
     */
    private synchronized void setState(int state) {
        if (D) Log.d(TAG, "setState() " + mState + " -> " + state);
        mState = state;

        // Give the new state to the Handler so the UI Activity can update
        mHandler.obtainMessage(RemoteBluetooth.MESSAGE_STATE_CHANGE, state, -1).sendToTarget();
    }

    /**
     * Return the current connection state. */
    public synchronized int getState() {
        return mState;
    }

    /**
     * Start the chat service. Specifically start AcceptThread to begin a
     * session in listening (server) mode. Called by the Activity onResume() */
    public synchronized void start() {
        if (D) Log.d(TAG, "start");

        // Cancel any thread attempting to make a connection
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        setState(STATE_LISTEN);
    }

    /**
     * Start the ConnectThread to initiate a connection to a remote device.
     * @param device  The BluetoothDevice to connect
     */
    public synchronized void connect(BluetoothDevice device) {
        if (D) Log.d(TAG, "connect to: " + device);

        // Cancel any thread attempting to make a connection
        if (mState == STATE_CONNECTING) {
            if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
        }

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        // Start the thread to connect with the given device
        mConnectThread = new ConnectThread(device);
        mConnectThread.start();
        setState(STATE_CONNECTING);
    }

    /**
     * Start the ConnectedThread to begin managing a Bluetooth connection
     * @param socket  The BluetoothSocket on which the connection was made
     * @param device  The BluetoothDevice that has been connected
     */
    public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {
        if (D) Log.d(TAG, "connected");

        // Cancel the thread that completed the connection
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}

        // Cancel any thread currently running a connection
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        // Start the thread to manage the connection and perform transmissions
        mConnectedThread = new ConnectedThread(socket);
        mConnectedThread.start();

        // Send the name of the connected device back to the UI Activity
        Message msg = mHandler.obtainMessage(RemoteBluetooth.MESSAGE_DEVICE_NAME);
        Bundle bundle = new Bundle();
        bundle.putString(RemoteBluetooth.DEVICE_NAME, device.getName());
        msg.setData(bundle);
        mHandler.sendMessage(msg);

        // save connected device
        //mSavedDevice = device;
        // reset connection lost count
        //mConnectionLostCount = 0;

        setState(STATE_CONNECTED);
    }

    /**
     * Stop all threads
     */
    public synchronized void stop() {
        if (D) Log.d(TAG, "stop");
        if (mConnectThread != null) {mConnectThread.cancel(); mConnectThread = null;}
        if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

        setState(STATE_NONE);
    }

    /**
     * Write to the ConnectedThread in an unsynchronized manner
     * @param out The bytes to write
     * @see ConnectedThread#write(byte[])
     */
    public void write(byte[] out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }

    public void write(int out) {
        // Create temporary object
        ConnectedThread r;
        // Synchronize a copy of the ConnectedThread
        synchronized (this) {
            if (mState != STATE_CONNECTED) return;
            r = mConnectedThread;
        }
        // Perform the write unsynchronized
        r.write(out);
    }

    /**
     * Indicate that the connection attempt failed and notify the UI Activity.
     */
    private void connectionFailed() {
        setState(STATE_LISTEN);

        // Send a failure message back to the Activity
        Message msg = mHandler.obtainMessage(RemoteBluetooth.MESSAGE_TOAST);
        Bundle bundle = new Bundle();
        bundle.putString(RemoteBluetooth.TOAST, "Unable to connect device");
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    /**
     * Indicate that the connection was lost and notify the UI Activity.
     */
    private void connectionLost() {
//        mConnectionLostCount++;
//        if (mConnectionLostCount < 3) {
//          // Send a reconnect message back to the Activity
//          Message msg = mHandler.obtainMessage(RemoteBluetooth.MESSAGE_TOAST);
//          Bundle bundle = new Bundle();
//          bundle.putString(RemoteBluetooth.TOAST, "Device connection was lost. Reconnecting...");
//          msg.setData(bundle);
//          mHandler.sendMessage(msg);
//          
//          connect(mSavedDevice);      
//        } else {
            setState(STATE_LISTEN);
            // Send a failure message back to the Activity
            Message msg = mHandler.obtainMessage(RemoteBluetooth.MESSAGE_TOAST);
            Bundle bundle = new Bundle();
            bundle.putString(RemoteBluetooth.TOAST, "Device connection was lost");
            msg.setData(bundle);
            mHandler.sendMessage(msg);
//        }
    }

    /**
     * This thread runs while attempting to make an outgoing connection
     * with a device. It runs straight through; the connection either
     * succeeds or fails.
     */
    private class ConnectThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final BluetoothDevice mmDevice;

        public ConnectThread(BluetoothDevice device) {
            mmDevice = device;
            BluetoothSocket tmp = null;

            // Get a BluetoothSocket for a connection with the
            // given BluetoothDevice
            try {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
            } catch (IOException e) {
                Log.e(TAG, "create() failed", e);
            }
            mmSocket = tmp;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectThread");
            setName("ConnectThread");

            // Always cancel discovery because it will slow down a connection
            mAdapter.cancelDiscovery();

            // Make a connection to the BluetoothSocket
            try {
                // This is a blocking call and will only return on a
                // successful connection or an exception
                mmSocket.connect();
            } catch (IOException e) {
                e.printStackTrace();
                connectionFailed();
                // Close the socket
                try {
                    mmSocket.close();
                } catch (IOException e2) {
                    Log.e(TAG, "unable to close() socket during connection failure", e2);
                }
                // Start the service over to restart listening mode
                BluetoothCommandService.this.start();
                return;
            }

            // Reset the ConnectThread because we're done
            synchronized (BluetoothCommandService.this) {
                mConnectThread = null;
            }

            // Start the connected thread
            connected(mmSocket, mmDevice);
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }

    /**
     * This thread runs during a connection with a remote device.
     * It handles all incoming and outgoing transmissions.
     */
    private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket) {
            Log.d(TAG, "create ConnectedThread");
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    int bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(RemoteBluetooth.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);
                mmOutStream.flush();
                // Share the sent message back to the UI Activity
//                mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
//                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void write(int out) {
            try {
                mmOutStream.write(out);

                // Share the sent message back to the UI Activity
//                mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer)
//                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                mmOutStream.write(EXIT_CMD);
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }
}

这是我的Activity

public class RemoteBluetooth extends Activity {

    // Layout view
    private TextView mTitle;

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;

    // Message types sent from the BluetoothChatService Handler
    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;

    // Key names received from the BluetoothCommandService Handler
    public static final String DEVICE_NAME = "device_name";
    public static final String TOAST = "toast";

    // Name of the connected device
    private String mConnectedDeviceName = null;
    // Local Bluetooth adapter
    private BluetoothAdapter mBluetoothAdapter = null;
    // Member object for Bluetooth Command Service
    private BluetoothCommandService mCommandService = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set up the window layout
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        // Set up the custom title
        mTitle = (TextView) findViewById(R.id.title_left_text);
        mTitle.setText(R.string.app_name);
        mTitle = (TextView) findViewById(R.id.title_right_text);

        // Get local Bluetooth adapter
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        // If the adapter is null, then Bluetooth is not supported
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        // If BT is not on, request that it be enabled.
        // setupCommand() will then be called during onActivityResult
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
        }
        // otherwise set up the command service
        else {
            if (mCommandService==null)
                setupCommand();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Performing this check in onResume() covers the case in which BT was
        // not enabled during onStart(), so we were paused to enable it...
        // onResume() will be called when ACTION_REQUEST_ENABLE activity returns.
        if (mCommandService != null) {
            if (mCommandService.getState() == BluetoothCommandService.STATE_NONE) {
                mCommandService.start();
            }
        }
    }

    private void setupCommand() {
        // Initialize the BluetoothChatService to perform bluetooth connections
        mCommandService = new BluetoothCommandService(this, mHandler);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mCommandService != null)
            mCommandService.stop();
    }

    private void ensureDiscoverable() {
        if (mBluetoothAdapter.getScanMode() !=
            BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
            startActivity(discoverableIntent);
        }
    }

    // The Handler that gets information back from the BluetoothChatService
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                case BluetoothCommandService.STATE_CONNECTED:
                    mTitle.setText(R.string.title_connected_to);
                    mTitle.append("HC-06");
                    break;
                case BluetoothCommandService.STATE_CONNECTING:
                    mTitle.setText(R.string.title_connecting);
                    break;
                case BluetoothCommandService.STATE_LISTEN:
                case BluetoothCommandService.STATE_NONE:
                    mTitle.setText(R.string.title_not_connected);
                    break;
                }
                break;
            case MESSAGE_DEVICE_NAME:
                // save the connected device's name
                mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                Toast.makeText(getApplicationContext(), "Connected to "
                               + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                break;
            case MESSAGE_TOAST:
                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                               Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(address);
                // Attempt to connect to the device
                mCommandService.connect(device);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupCommand();
            } else {
                // User did not enable Bluetooth or an error occured
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.scan:
            // Launch the DeviceListActivity to see devices and do scan
            Intent serverIntent = new Intent(this, DeviceListActivity.class);
            startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
            return true;
        case R.id.discoverable:
            // Ensure this device is discoverable by others
            ensureDiscoverable();
            return true;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {

            String blinkCommand = "&$V00X77V0" ;
            String empty = "";
            for (int i = 0 ; i < (100 - blinkCommand.length()) ; i ++){
                empty += "0";
            }
            String limiter = "\r\n";

            String fullCommand = blinkCommand +  empty + limiter;
            mCommandService.write(fullCommand.getBytes());
    //      mCommandService.write(BluetoothCommandService.VOL_UP);
            return true;
        }
        else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
            String blinkCommand = "&$V00X77V0" + "\r\n";
            mCommandService.write(blinkCommand.getBytes());
        //  mCommandService.write(BluetoothCommandService.VOL_DOWN);
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}

自 android 4.2 以来,蓝牙堆栈发生了变化,所以我猜您正在 android>=4.2 上进行测试。

你的问题就在这里tmp = device.createRfcommSocketToServiceRecord(MY_UUID);。此套接字创建方法从 4.2 开始不兼容,因此在失败后您需要使用后备方法:tmp =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);

不用担心called with no BluetoothManagerCallback被扔了,没关系。

所以如果你这样做,它会起作用:

        try {
            socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
        } catch (Exception e) {Log.e("","Error creating socket");}

        try {
            socket.connect();
            Log.e("","Connected");
        } catch (IOException e) {
            Log.e("",e.getMessage());
            try {
                Log.e("","trying fallback...");

                socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
                socket.connect();

                Log.e("","Connected");
            }

另外,我回答了here https://stackoverflow.com/a/25647197/1639215更详细,关于类似的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

对于 Android Nexus 5,调用 getBluetoothService() 时无需使用 BluetoothManagerCallback 的相关文章

  • Node.js 服务器超时问题(EC2 + Express + PM2)

    我对运行生产 Node js 应用程序还比较陌生 最近我遇到了服务器超时的问题 基本上在一定的使用量和时间后 我的 Node js 应用程序停止响应请求 我什至不再看到在我的控制台上触发路由 就好像整个事情都停止了 来自我的客户端 运行 A
  • 每个连接请求都被视为直接连接请求+ android ble

    我们正在编写一个 ble 应用程序 希望与我们想要连接的外设建立持久连接 出于同样的目的 每当我们失去现有连接时 我们都希望与外围设备重新连接 因此 我们作为中心的 Android 应用程序只是尝试通过调用 bluetoothdevice
  • Android 低功耗蓝牙:characteristic.getPermissions() 返回 0?

    我正在编写一个Android BLE应用程序 我正在尝试获取某个特性的权限 我已经设法使用characteristic getProperties 获取特征属性 并且它返回一个非零值 但是 当我使用getPermission 方法时 它返回
  • 通过 BLE 传输大数据块的最佳方法

    我是 BLE 的新手 希望您能够指出我正确的实施方法 我正在开发一个应用程序 其中外围 电池供电 设备不断聚合传感器读数 在移动端应用程序上会有一个 同步 按钮 按下按钮后 我想将外围设备中积累的所有传感器读数传输到移动应用程序 同步之间的
  • 为什么服务器运行时PHP ftp_connect失败?

    一段时间以来 我一直在尝试通过 FTP 连接到我的服务器 但似乎无法正常工作 connection id ftp connect example com 22 运行此代码时 它会挂起一段时间 直到 PHP 最终告诉我脚本执行时间太长并退出
  • AngularJS $http 服务请求的默认超时是多少?

    In http 文档 https docs angularjs org api ng service 24http它没有提及未定义情况下的默认超时 我如何知道此配置的默认值是什么 曾经 http 请求的默认超时为 30 秒 但现在许多浏览器
  • 如何同时有效地读取两个 BLE 设备的温度?

    首先 我正在使用RxAndroidBLE http polidea github io RxAndroidBle 管理我的 BLE 连接的库 我有两个我想同时读取两者的温度 例如 我想每 500 毫秒读取两个设备的温度 并将其在两个 Tex
  • 蓝牙无法在后台模式下扫描附近的设备 Android 11+

    我尝试在后台模式下扫描附近的蓝牙设备 但它在 Android 11 等某些设备上不起作用 这是我的示例代码 在前台工作得很好 授予所有权限
  • Android 设备可以充当 iBeacon 吗?

    Android 设备能否充当 iBeacon 并确定其他 Android 设备何时进入其范围 其他 Android 设备需要打开蓝牙吗 如果顾客来到我的商店 但他的设备上没有安装我的应用程序 iBeacon 可以工作吗 还是必须先安装该应用
  • Microsoft Hive ODBC 驱动程序 2.1.5 超时

    我最近升级到 Microsoft hive odbc 驱动程序版本 2 01 05 1006 于 2016 年 12 月 8 日发布 我的代码适用于驱动程序的先前版本 1 0 现在 当运行我的代码时 我不断收到以下错误 错误 HY000 M
  • Secure.getString(mContext.getContentResolver(), "bluetooth_address") 在 Android O 中返回 null

    当我尝试通过这种方式获取 Android O 设备上的蓝牙地址时 private String getBlutoothAddress Context mContext Check version API Android BluetoothA
  • Android 蓝牙 connect() 抛出错误

    我有一个项目要求 其中我们有一个包含两个组件的产品 一个 Android 平板电脑和一个 PCB 包含 RN42 蓝牙芯片 这两个组件位于塑料外壳内 因此总是彼此靠近 这两个组件设备最初会配对一次 因此配对信息将永远存在于平板电脑中 Pro
  • pymodbus TcpClient超时

    我遇到 pymodbus TcpClient 超时问题 import logging from pymodbus client sync import ModbusTcpClient logging basicConfig log logg
  • 通过蓝牙打印机 Android 打印 Pdf 文件

    在我的项目中需要通过蓝牙打印机打印Pdf文件 我写了一个代码通过pdf打印 它对于文本来说很好 但我想在蓝牙打印机上打印PDF文件 我的打印文本的java代码 Override public void onCreate Bundle sav
  • 蓝牙连接无需配对

    连接蓝牙设备的正常方式是通过配对 我们需要以非正常方式连接到设备 仅使用蓝牙 MAC 地址 我们不希望系统提示输入 PIN 我们知道该设备支持此技术 但我们找不到在 Android 上执行此操作的方法 缩写代码如下所示 String mac
  • BroadcastReceiver 和 ACTION_BOND_STATE_CHANGED 在 Android 9.0 中部分工作

    我试图通过广播接收器捕获与 android 的配对过程中的事件 看起来 BluetoothDevice BOND BONDING是有效的 但是BluetoothDevice BOND BONDED not 在旧的 Android 版本中 此
  • 什么是http请求期间的连接超时

    我找到了关于 连接超时 的两种解释 当客户端在 timeout 秒内未向服务器发送任何字节时 服务器将关闭套接字连接 它似乎与 HTTP 标头有一些关系 Connection keep alive 如果在此期间未建立套接字连接 则客户端将在
  • Android 蓝牙低功耗特性通知计数限制:这是否因设备而异?

    Context 我正在构建一个针对 5 0 的 Android 应用程序 它使用 BLE 连接到多个外围设备 这些外设中的每一个都有许多 10 个特性 理想情况下 我想订阅这些特征中每一项的更改通知 然而 我从阅读中了解到 Android
  • 我在socket上设置了超时,发现这个值不能大于21

    我在socket上设置了超时 该值小于21秒才有效 21秒后发现超时还是21秒 public static void main String args SimpleDateFormat sdf new SimpleDateFormat yy
  • BLE 外设支持 Android-L 示例 [关闭]

    Closed 这个问题需要调试细节 help minimal reproducible example 目前不接受答案 我希望有一个适用于 Android L 的 BLE 外设模式的示例 我的代码给了我奇怪的错误 即广告商太多 这没有任何意

随机推荐

  • 从数据库中检索图像

    我正在开发一个显示员工列表的项目 此处将显示该员工的信息和照片 我的项目现在可以在列表框中显示员工列表 当我双击员工时 他 她的个人资料将显示在文本框中 我的问题是我无法让他们的照片显示在picturebox 我已经将他们的照片以及他们的
  • 绘制 dr4pl 剂量反应曲线,以及如何将它们与 ggplot2 集成?

    我正在尝试建立一种高通量方法来绘制大型筛选实验的剂量反应曲线 Prism 显然有最简单的方法可以很好地绘制剂量反应曲线 但我无法复制和粘贴这么多数据 自从 CRAN 被移除后drc 包dr4pl似乎是可行的方法 但目前可用的指导还很少 ma
  • 在python中解压嵌套的zip文件

    我正在寻找一种在 python 中解压缩嵌套 zip 文件的方法 例如 考虑以下结构 为方便起见 使用假设名称 Folder ZipfileA zip 压缩文件A1 zip 压缩文件A2 zip ZipfileB zip 压缩文件B1 zi
  • 在 C 中将整数转换为二进制

    我正在尝试将整数 10 转换为二进制数 1010 此代码尝试执行此操作 但我在 strcat 上遇到段错误 int int to bin int k char bin bin char malloc sizeof char while k
  • 为多面图中的单个面板添加几何图层

    从以下链接中获取提示使用 ggplot2 对齐两个图 http rwiki sciviews org doku php id tips graphics ggplot2 aligntwoplots 我能够根据共同的 x 轴绘制 2 个 y
  • 流明:启用 CORS

    我使用 Lumen 构建了一个 API 并希望使用 JavaScript 和 XMLHttpRequest 对象访问它 但每次我的 PUT GET POST 和 DELETE 请求都会转化为选项 请求 我看了很多网站都有CORS的信息 我构
  • 点击按钮后 Python Tkinter 销毁标签

    我有一个在单击按钮后出现的标签 但是每次单击按钮后 前一个标签仍保留在其位置并创建一个新标签 我希望将新标签代替旧标签 代码如下 browser webdriver PhantomJS browser get http www ipvoid
  • iPhone硬计算和缓存

    我有问题 我有数据库500k记录 每个记录存储纬度 经度 动物种类 观察日期 我必须在 Mapkit 视图上方绘制网格 15x10 以显示该网格单元中物质的浓度 每个单元格都是 32x32 的盒子 如果我在运行时计算它是非常slow 有人知
  • Cocoa:如何将布尔属性绑定到 NSCellStateValue?

    我想绑定布尔值enabled的财产NSTextField到一个状态NSButton 我已经尝试添加自定义NSValueTransformer从而改变了状态NSButton into NSNumber 但是 在这种情况下 由于某种原因 文本字
  • 有没有办法通过命令行将 JVM 参数传递给 Maven? [复制]

    这个问题在这里已经有答案了 可能的重复 Maven Jetty 插件 如何控制 VM 参数 https stackoverflow com questions 2007192 maven jetty plugin how to contro
  • 如何在 Java 中对 HTTP 请求中的西里尔字母进行编码?

    美好时光 我的 Adroid 应用程序向 Google 的 API 服务之一执行 HTTP 请求 当然 当请求的参数是英文时 它可以工作 但是当我用西里尔文测试我的函数时 我收到 400 错误 似乎问题是将 Win 1251 字符串编码为
  • 将数据从 Azure HUB-IOT 保存到 Azure SQL 数据库

    我最近创建了一个 Azure Hub IOT 其中我从虚拟设备发送一些数据 我知道数据已到达 因为我可以从终端看到它们 但现在我想获取这些数据并将它们保存到 Azure SQL DataBase 中 我怎样才能做到这一点 如果有人可以向我解
  • 如何 git Blame 目录

    我想知道如何使用 gitblame 来知道谁创建了单个目录 当我尝试时 git blame DIRECTORY NAME I get fatal no such path DIRECTORY NAME in HEAD 顺便说一句 该目录是空
  • 向 Windows 窗体消息循环发送或发布消息

    我有一个线程从命名管道读取消息 它是阻塞读取 这就是它在自己的线程中的原因 当该线程读取消息时 我希望它通知主线程中运行的 Windows 窗体消息循环消息已准备就绪 我怎样才能做到这一点 在win32中我会做一个PostMessage 但
  • 无法加密特殊字符:不是块长度的倍数

    我有一个加密 解密算法 我试图加密一些特殊字符从android发送到服务器 但它抛出一个异常 java lang Exception 加密 错误 0607F08A 数字信封例程 EVP EncryptFinal ex 数据不是块长度的倍数
  • 如何从 UnsafeMutableRawPointer 中获取字节?

    如何访问由 C API Core Audio 等 传递给 Swift 函数的 UnsafeMutableRawPointer Swift 3 中的新功能 指向的内存之外的字节 或 Int16 浮点数等 load
  • R代码:如何根据其他变量的多个条件生成变量

    我有一个 R 初学者用户 这是我的数据集 factor1 lt c 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 factor2 lt c 1 2 3 4 5 6 7 8 9 10 11 12 13 1
  • 需要帮助设置一个界面,其中大多数元素的旋转都是固定的,但警报和共享框会随设备自动旋转

    我正在使用 Xcode 7 和 Swift 2 我正在开发一个带有相机预览层和控件的界面 其显示方式类似于本机 iOS 相机应用程序 当您转动设备时 所有控件都保持在原位 但图标会 旋转 到位以根据设备方向正确定位 我希望我以一种有意义的方
  • 使用 Maven 执行器跳过模块

    我在用着enforcer我在根 pom 的多节点项目中添加了一个插件 但我有一个测试模块 我并不关心在那里运行该插件 因为它不会创建任何 jar 并且仅用于测试目的 有没有办法跳过插件配置中的模块之一 检查文档我找不到任何东西 只是如何禁止
  • 对于 Android Nexus 5,调用 getBluetoothService() 时无需使用 BluetoothManagerCallback

    我将实现一个模块 用于通过蓝牙从 Android 智能手机向 HC 06 发送命令 当执行时 出现以下异常 并且找不到标题所示的错误消息的线索 请问修改方法可以吗 异常日志消息 07 29 13 51 37 701 W BluetoothA