簡體   English   中英

Android藍牙spp讀寫速度太慢

[英]Android bluetooth spp reads and writes too slow

我正在嘗試制作一個為我的計算機充當無線鼠標的android應用。 我決定將其用於藍牙和Wifi(用戶選擇。)Wifi的工作原理異常出色,但藍牙的響應時間卻很長。 我要發送的只是一堆2字節的數組,所以我不明白為什么它這么慢。 這是我的發送和接收代碼(發送在單獨的線程上)

 package com.tutorials.jurko.androidmouse;

import android.bluetooth.BluetoothSocket;
import android.net.ConnectivityManager;

import java.io.BufferedOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Jurko on 14/02/2015.
 */
public class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    public static int count = 0;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            tmpIn = mmSocket.getInputStream();
            tmpOut = mmSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void write(Byte[] bytes) {
        count++;
        try {
            byte x = bytes[0].byteValue();
            byte y = bytes[1].byteValue();
            System.out.println("Count: " + count);
            byte buf[] = {x, y};
            mmOutStream.write(buf);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

接收代碼:

import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.*;

/**
 * Class that implements an SPP Server which accepts single line of
 * message from an SPP client and sends a single line of response to the client.
 */
public class SimpleSPPServer  {

    byte dx;
    byte dy;

    //start server
    private void startServer() throws IOException, AWTException {

        Robot r = new Robot();

        //Create a UUID for SPP
        UUID uuid = new UUID("1101", true);
        //Create the servicve url
        String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";

        //open server url
        StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );

        //Wait for client connection
        System.out.println("\nServer Started. Waiting for clients to connect...");

        StreamConnection connection=streamConnNotifier.acceptAndOpen();

        RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
        System.out.println("Remote device address: "+dev.getBluetoothAddress());
        System.out.println("Remote device name: "+dev.getFriendlyName(true));

        //read string from spp client
        InputStream inStream=connection.openInputStream();
        BufferedInputStream bis = new BufferedInputStream(inStream);
        byte[] lineRead = new byte[2];

        while(true)  {
            int available = bis.available();
            if (available > 0) {
                bis.read(lineRead,0,2);

                System.out.println(lineRead[0] + " " + lineRead[1]);

                PointerInfo a = MouseInfo.getPointerInfo();
                Point b = a.getLocation();
                int x = (int)b.getX();
                int y = (int)b.getY();

                dx = lineRead[0];
                dy = lineRead[1];

                int newX = x + dx;
                int newY = y + dy;

                if(dx == -98 && dy == -98) {
                    // Right click
                    r.mousePress(InputEvent.BUTTON3_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);

                } else if (dx == -99 && dy == -99) {
                    // Left click
                    r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
                } else if (dx == -97 && dy == -97) {
                    // Left click
                    r.mousePress(InputEvent.BUTTON2_DOWN_MASK);
                    r.mouseRelease(InputEvent.BUTTON2_DOWN_MASK);
                } else {
                    for (int i = 0; i < 10; i++) {
                        r.mouseMove(newX + i * dx, newY + i *dy);
                        r.delay(10);
                    }
                }
            }

        }

    }

    public static void main(String[] args) throws IOException, AWTException {

        //display local device address and name
        LocalDevice localDevice = LocalDevice.getLocalDevice();
        System.out.println("Address: "+localDevice.getBluetoothAddress());
        System.out.println("Name: "+localDevice.getFriendlyName());

        SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
        sampleSPPServer.startServer();

    }
}

我的猜測是,沿着代碼的某個地方,您的代碼正在引起藍牙掃描或其他繁重的BT操作。 例如:

RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));

我相信這三行會導致BT掃描發生。 由於它只是調試,因此建議您刪除它們,看看是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM