如何在C for Android中正确连接蓝牙服务器?

问题描述 投票:0回答:1

我正在尝试连接计算机和Android应用程序之间的应用程序。该应用程序将是客户端,计算机将是服务器。

在服务器上使用BlueZ(Linux上的蓝牙C库):

#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>

int main(int argc, char **argv)
{
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
    char buf[1024] = { 0 };
    int s, client, bytes_read;
    socklen_t opt = sizeof(rem_addr);

    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

    // bind socket to port 1 of the first available 
    // local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = *BDADDR_ANY;
    loc_addr.rc_channel = (uint8_t) 1;
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));

    // put socket into listening mode
    listen(s, 1);

    // accept one connection
    client = accept(s, (struct sockaddr *)&rem_addr, &opt);

    ba2str( &rem_addr.rc_bdaddr, buf );
    fprintf(stderr, "accepted connection from %s\n", buf);
    memset(buf, 0, sizeof(buf));

    // read data from the client
    bytes_read = read(client, buf, sizeof(buf));
    if( bytes_read > 0 ) {
        printf("received [%s]\n", buf);
    }

    // close connection
    close(client);
    close(s);
    return 0;
}

这个例子的来源是:http://people.csail.mit.edu/albert/bluez-intro/x502.html

我用来连接的应用程序的类是:

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Message;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.UUID; 

public class ConnectionThread extends Thread {


    BluetoothSocket btSocket = null;
    BluetoothServerSocket btServerSocket = null;
    InputStream input = null;
    OutputStream output = null;
    String btDevAddress = null;
    String myUUID = "00000101-0000-1000-8000-00805F9C34BF";
    boolean server;
    boolean running = false;
    public ConnectionThread() {

        this.server = true;
    }
    public ConnectionThread(String btDevAddress) {

        this.server = false;
        this.btDevAddress = btDevAddress;
    }
    public void run() {

        this.running = true;
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();

        if(this.server) {
            try {
                btServerSocket = btAdapter.listenUsingRfcommWithServiceRecord("Super Bluetooth", UUID.fromString(myUUID));
                btSocket = btServerSocket.accept();
                if(btSocket != null) {

                    btServerSocket.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
                toMainActivity("---N".getBytes());
            }


        } else {

            try {

                BluetoothDevice btDevice = btAdapter.getRemoteDevice(btDevAddress);
                btSocket = btDevice.createRfcommSocketToServiceRecord(UUID.fromString(myUUID));

                btAdapter.cancelDiscovery();

                if (btSocket != null)
                    btSocket.connect();

            } catch (IOException e) {
                e.printStackTrace();
                toMainActivity("---N".getBytes());
            }

        }

        if(btSocket != null) {

            toMainActivity("---S".getBytes());

            try {
                input = btSocket.getInputStream();
                output = btSocket.getOutputStream();

                byte[] buffer = new byte[1024];
                int bytes;

                while(running) {

                    bytes = input.read(buffer);
                    toMainActivity(Arrays.copyOfRange(buffer, 0, bytes));

                }

            } catch (IOException e) {
                e.printStackTrace();
                toMainActivity("---N".getBytes());
            }
        }

    }
    private void toMainActivity(byte[] data) {

        Message message = new Message();
        Bundle bundle = new Bundle();
        bundle.putByteArray("data", data);
        message.setData(bundle);
        MainActivity.handler.sendMessage(message);
    }
    public void write(byte[] data) {

        if(output != null) {
            try {

                output.write(data);

            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            toMainActivity("---N".getBytes());
        }
    }


    public void cancel() {

        try {

            running = false;
            btServerSocket.close();
            btSocket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        running = false;
    }
}

要将数据发送到服务器,我会:

ConnectionThread connect = new ConnectionThread(data.getStringExtra("Address_server"));
connect.start();
byte[] data = "20".getBytes();
connect.write(data);

问题不在于连接正在发生。我相信这是因为服务器不在UUID中。

如何将UUID插入服务器以便它可以接收发送的数据?

android c bluetooth
1个回答
0
投票

我不知道如何在libbluetooth / raw socket中做到这一点。但您可以使用Bluez提供的配置文件管理器界面来创建自己的自定义配置文件并使用bluetoothd注册。

以下是从this presentation直接复制的示例幻灯片。

Profile Manager

要详细解释,您需要定义Profile1接口中描述的方法,并使用“RegisterProfile”方法将其注册到Bluetoothd。

我没有任何自定义配置文件实现示例,但您始终可以将bluez-alsa repository用于基于AG / HFP / HS的配置文件实现,该实现使用此接口实现为外部配置文件。

这个存储库中最有趣的部分是“register_profile”API,它采用UUID和其他必需参数。您可以复制相同的实现并注册您自己的UUID并实现,

NewConnection:新设备与服务器连接时需要执行的操作。您可以在Linux here中找到如何使用DBUS进行连接以及here的说明。

RequestDisconnection:当设备断开连接时,Bluetoothd会调用哪个。您可以对基于设备的分配/资源执行清理。

Release:当蓝牙退出时将调用哪个。您可以执行配置文件完全清理和UnregisterProfile以便正常退出。

© www.soinside.com 2019 - 2024. All rights reserved.