蓝牙设备扫描

时间:2016-11-25 01:40:54

标签: vb.net bluetooth serial-port bluetooth-lowenergy

我需要你的帮助。

请仔细阅读以下参考资料....

现状:我做了一个可以与PC串口通讯的应用程序,这个串口进一步连接到BLE模块,这样我就可以通过它发送和接收命令和响应....

问题:我必须制作一个窗口应用程序(仅限Visual Basic中的PC程序)作为列表扫描附近的蓝牙设备....然后,如果我选择任何一个,它将通过BLE连接到该设备。 我不是编程专家,所以寻求你的帮助。请建议一些代码工作来实现....

非常感谢。

2 个答案:

答案 0 :(得分:0)

通过枚举命名空间,您可以查找内部连接到系统,外部连接或可通过无线或网络协议检测到的设备。您用于枚举可能设备的API是(请参阅。https://msdn.microsoft.com/library/windows/apps/br225459)。

async void enumerateAvailableDevices() {
  DeviceInformationCollection collection = await DeviceInformation.FindAllAsync();
}

请记住在 Package.appxmanifest 中为蓝牙添加功能

答案 1 :(得分:0)

@Emlinux

我为打印机实施的这段代码。希望这能帮助你弄明白;)

 private string deviceName;
    private string rfCommName;
    private string rfCommId;
    private object rfCommProperty;
    private bool rfCommAvalability;
    private PeerInformation device;
    private HostName hostName;
    private StreamSocket streamSocket = null;
    private DataWriter dataWriter;
    private RfcommDeviceService rfcommService;
    private DeviceInformation deviceInformation;
    private DeviceInformationCollection deviceInformationCollection;
    private static readonly Guid RfcommSerialUuid = Guid.Parse("00001101-0000-1000-8000-00805f9b34fb");

    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {

    }

    private async void searchButton_Click(object sender, RoutedEventArgs e)
    {
        PeerFinder.AlternateIdentities["Bluetooth:PAIRED"] = "";
        var serviceInfoCollection = await PeerFinder.FindAllPeersAsync();
        if (serviceInfoCollection.Count > 0)
        {
            Debug.WriteLine("Search() - There's few paired devices");
            foreach (var peerInfo in serviceInfoCollection)
            {
               if (peerInfo.DisplayName.ToLower().IndexOf("star-") > -1)
               {
                    hostName = peerInfo.HostName;
                    deviceName = peerInfo.DisplayName;
                    device = peerInfo;
                    Debug.WriteLine("Search() - There's your star printer");
                    Debug.WriteLine("Search() - Device - " + deviceName);
                    Debug.WriteLine("Search() - Hostname - " + hostName);
                    textBlock.Text = "I've found ur printer: " + deviceName;
                    break;
               }
            }
        }
        else
        {
            Debug.WriteLine("Search() - There's no paired devices");
            textBlock.Text = "I didn't found ur printer";
        }
    }

    private async void rfcommButton_Clicked(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("RfComm() - I'm going to check all Rfcomm devices");
        deviceInformationCollection = await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.FromUuid(RfcommSerialUuid)));

        if (deviceInformationCollection != null)
        {
            foreach (var rfComm in deviceInformationCollection)
            {
                if (rfComm.Name.ToLower().IndexOf("star-") > -1)
                {
                    rfCommName = rfComm.Name;
                    rfCommId = rfComm.Id;
                    rfCommProperty = rfComm.Properties;
                    rfCommAvalability = rfComm.IsEnabled;
                    Debug.WriteLine("RfComm() -  name - " + rfCommName);
                    Debug.WriteLine("RfComm() -  id - " + rfCommId);
                    Debug.WriteLine("RfComm() -  property - " + rfCommProperty);
                    Debug.WriteLine("RfComm() -  avalability - " + rfCommAvalability);
                    deviceInformation = rfComm;
                }
            }
        }

        try
        {
            rfcommService = await RfcommDeviceService.FromIdAsync(rfCommId);
            Debug.WriteLine("RfComm() - seems like we're at home");
            textBlock.Text = "seems liek we're at home";
        }
        catch (Exception)
        {
            Debug.WriteLine("RfComm() - access to the device is denied");
            textBlock.Text = "access to the device is denied";
        }
    }

    private void socketButton_Click(object sender, RoutedEventArgs e)
    {
        if (streamSocket == null)
        {
            Debug.WriteLine("Socket() - socket's null");
            lock (this)
            {
                streamSocket = new StreamSocket();
                Debug.WriteLine("Socket() - socket created");
                textBlock.Text = "socket created";
            }
        }

        try
        {
            dataWriter = new DataWriter(streamSocket.OutputStream);
            Debug.WriteLine("Socket() - data writer created");
        }
        catch (Exception)
        {
            Debug.WriteLine("Socket() - something went wrong while creating writer");
            textBlock.Text = "something went wront while socket creation";
        }
    }

    private async void connectToButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Debug.WriteLine("Connect() - Trying to connect");
            await streamSocket.ConnectAsync(hostName, rfcommService.ConnectionServiceName, SocketProtectionLevel.BluetoothEncryptionAllowNullAuthentication);
            Debug.WriteLine("Connect() - Connected");
            textBlock.Text = "Connected";
        }
        catch (Exception)
        {
            Debug.WriteLine("Connect() - Couldn't connect");
            textBlock.Text = "Couldn't connect";
        }
    }
相关问题