2016-11-25 219 views
0

我需要你的幫助。藍牙設備掃描

請通過下面這個參考....

現狀:我做了可與PC機的串口通信和該串口進一步連接到BLE模塊,這樣我就可以發送和接收的應用通過它的命令和響應... ...

問題:我必須製作一個窗口應用程序(PC程序在Visual Basic中僅用於)掃描附近的藍牙設備列表....然後,如果我選擇任何一個它將通過BLE連接到該設備。 我不是編程專家,所以尋求你的幫助。請推薦一些代碼工作來實現....

非常感謝。

回答

0

枚舉名稱空間使您能夠查找內部連接到系統,外部連接或通過無線或網絡協議檢測到的設備。您用來通過可能的設備枚舉的API是(請參閱。https://msdn.microsoft.com/library/windows/apps/br225459)。

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

請記住在添加能力 Package.appxmanifest藍牙

+0

感謝魯德尼茨基您的建議.....我會嘗試這個.... 但到目前爲止,我嘗試了這種方式。由於我的BLE模塊在AT命令上工作,所以我可以通過發送AT掃描命令並在我的richtextbox中接收附近的設備地址來更改其模式以感應掃描。現在我需要從richtextbox中選擇任意一個設備字符串,同時點擊鼠標點擊它,並在點擊附近設備的特定BT地址(接收字符串)時做一些進一步的操作。我完全瞭解VB來實現這一點。請幫幫我。謝謝 – Emlinux

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"; 
     } 
    }