12

これが最後のサーバーであるWindowsでBluecoveを使用して、Android(SDKのAPIを使用)からPCにデータを送信しようとしています。

Android をサーバーに接続することはできますが、ソケットの出力ストリームに書き込むと、サーバー上で何も起こりません。onPut メソッドをオーバーライドしましたが、呼び出されません。

コードは次のとおりです。誰かが私を助けることができれば、私は非常に感謝しています:

アンドロイド

public class BluetoothWorker {

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

private static BluetoothSocket socket;


private static BluetoothSocket getBluetoothSocket(){

    BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    mBluetoothAdapter.cancelDiscovery();
    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    // If there are paired devices
    if (pairedDevices.size() > 0) {
        // Loop through paired devices
        for (BluetoothDevice device : pairedDevices) {
            // Add the name and address to an array adapter to show in a ListView
            if(device.getName().equalsIgnoreCase(("MIGUEL-PC"))){
                try {
                    return device.createRfcommSocketToServiceRecord(generalUuid);
                } catch (IOException e) {
                    return null;
                }
            }
        }
    }
    return null;
}

public static boolean sendData(String status){

        socket = getBluetoothSocket();
        try {
            socket.connect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            socket = null;
        }

    if(socket != null){
        try {       

            socket.getOutputStream().write(status.getBytes());
            socket.getOutputStream().flush();
            socket.close();
            return true;
        } catch (IOException e) {
            socket = null;
            return false;
        }
    }else{
        return false;
    }
}
}

PC コード

public class AISHIntegrationBTBridge {

static final String serverUUID = "0000110100001000800000805F9B34FB";

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

    LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);

    SessionNotifier serverConnection = (SessionNotifier) Connector.open("btgoep://localhost:"
            + serverUUID + ";name=ObexExample");

    int count = 0;
    while(count < 2) {
        RequestHandler handler = new RequestHandler();
        serverConnection.acceptAndOpen(handler);

        System.out.println("Received OBEX connection " + (++count));
    }
}

private static class RequestHandler extends ServerRequestHandler {

    public int onPut(Operation op) {
        try {
            HeaderSet hs = op.getReceivedHeaders();
            String name = (String) hs.getHeader(HeaderSet.NAME);
            if (name != null) {
                System.out.println("put name:" + name);
            }

            InputStream is = op.openInputStream();

            StringBuffer buf = new StringBuffer();
            int data;
            while ((data = is.read()) != -1) {
                buf.append((char) data);
            }

            System.out.println("got:" + buf.toString());

            op.close();
            return ResponseCodes.OBEX_HTTP_OK;
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
        }
    }
}
}
4

2 に答える 2

8

私は2年前に同様のことをしました。

Android の場合、私のコードはあなたのものとは少し異なります。

BluetoothSocket socket = Device.createRfcommSocketToServiceRecord(device_UUID);
socket.connect();
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

dos.writeChar('x'); // for example

socket.close();

PCにDataOutputStreamデータを送っていました。しかし、確かにこれは問題ではありません。参考までに。

パソコンの場合、

LocalDevice localDevice = LocalDevice.getLocalDevice();

localDevice.setDiscoverable(DiscoveryAgent.GIAC); // Advertising the service

String url = "btspp://localhost:" + device_UUID + ";name=BlueToothServer";
StreamConnectionNotifier server = (StreamConnectionNotifier) Connector.open(url);

StreamConnection connection = server.acceptAndOpen(); // Wait until client connects
//=== At this point, two devices should be connected ===//
DataInputStream dis = connection.openDataInputStream();

char c;
while (true) {
    c = dis.readChar();
    if (c == 'x')
        break;
}

connection.close();

これは 2 年前に行われたため、上記のコードが現在でも機能するかどうかはわかりません。BlueCove API は大幅に変更された可能性があります。とにかく、これらのコードは私にとってはうまくいきます。これがお役に立てば幸いです。

もう 1 つの注意点は、BlueCove を使用するには、PC で東芝 Bluetooth ドライバーをアンインストールし、Microsoft ドライバーを再インストールする必要があったことです。そうしないと、うまくいきません。(ただし、BlueCove の最新バージョンは既に別のドライバーをサポートしている可能性があります。何か間違っていたら訂正してください。)

于 2011-10-21T10:25:53.233 に答える
0

Android の sendData メソッドで「write」の代わりに「println」を試してください。

于 2011-10-24T10:34:15.003 に答える