Answered by:
Windows Phone 8, Visual Basic, Bluetooth

Question
-
Hi All
I have an existing Android app that receives data from a bluetooth device (classic) and reacts accordingly. I need to rewrite it for windows phone 8. I am having trouble getting the Windows.Devices.Bluetooth name space to do anything. I am new to windows phone development so it's feeling a bit strange.
My objective is to have a background task running to listen for the connection and receive the data. Once the data is received I can then process.
I believe this will be a challenge :)
Any help would be appreciated.
Regards
John.
Below is some code
Imports Windows.Devices.Bluetooth Public Class ursosbutton Dim bt As BluetoothClassOfDevice Public Sub New() bt.New() <--- ERROR End Sub Public Function cod() As String Try Return bt.ToString Catch Return "ERROR" End Try End Function End Class
Error 1 Type 'Windows.Devices.Bluetooth.BluetoothClassOfDevice' has no constructors. C:\Users\John Murphy\Documents\Visual Studio 2013\Projects\App7\App7\clsBT.vb 9 9 myfirstapp
Wednesday, April 8, 2015 12:17 PM
Answers
-
Hi Jmu5667,
Not sure if its too late to reply you. But looks like if you have the C# code, translate to VB.NET code is not a difficult thing.
You could find some convert engine for help if you search on the Internet, here I tried to convert some from the documentation Siddhanath provided. Hope helps.
Connect to a peer app
Private Sub AppToApp() ' PeerFinder.Start() is used to advertise our presence so that peers can find us. ' It must always be called before FindAllPeersAsync. PeerFinder.Start() Dim peers = Await PeerFinder.FindAllPeersAsync() If peers.Count = 0 Then Debug.WriteLine("Peer not found.") Else ' Select a peer. In this example, let's just pick the first peer. Dim selectedPeer As PeerInformation = peers(0) ' Attempt a connection Dim streamSocket = Await PeerFinder.ConnectAsync(selectedPeer) DoSomethingUseful(streamSocket) End If End Sub
Connect to a device
Private Sub AppToDevice() ' Configure PeerFinder to search for all paired devices. PeerFinder.AlternateIdentities("Bluetooth:Paired") = "" Dim pairedDevices = Await PeerFinder.FindAllPeersAsync() If pairedDevices.Count = 0 Then Debug.WriteLine("No paired devices were found.") Else ' Select a paired device. In this example, just pick the first one. Dim selectedDevice As PeerInformation = pairedDevices(0) ' Attempt a connection Dim socket As New StreamSocket() ' Make sure ID_CAP_NETWORKING is enabled in your WMAppManifest.xml, or the next ' line will throw an Access Denied exception. ' In this example, the second parameter of the call to ConnectAsync() is the RFCOMM port number, and can range ' in value from 1 to 30. Await socket.ConnectAsync(selectedDevice.HostName, "1") DoSomethingUseful(socket) End If End Sub
Listen for a connection request
' Page Constructor Public Sub New() InitializeComponent() AddHandler Loaded, AddressOf MainPage_Loaded End Sub Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) AddHandler PeerFinder.ConnectionRequested, AddressOf PeerFinder_ConnectionRequested End Sub Private Sub PeerFinder_ConnectionRequested(sender As Object, args As ConnectionRequestedEventArgs) If ShouldConnect() Then ' Go ahead and connect ConnectToPeer(args.PeerInformation) End If End Sub Private Sub ConnectToPeer(peer As PeerInformation) Dim socket As StreamSocket = Await PeerFinder.ConnectAsync(peer) DoSomethingUseful(socket) End Sub Private Function ShouldConnect() As Boolean ' Determine whether to accept this connection request and return Return True End Function
Detect the state of the Bluetooth radio on your phone
Private Sub FindPaired() ' Search for all paired devices PeerFinder.AlternateIdentities("Bluetooth:Paired") = "" Try ' Handle the result of the FindAllPeersAsync call Dim peers = Await PeerFinder.FindAllPeersAsync() Catch ex As Exception If CUInt(ex.HResult) = &H8007048fUI Then MessageBox.Show("Bluetooth is turned off") End If End Try End Sub
--JamesWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Jamles Hez Wednesday, April 22, 2015 2:03 PM
- Marked as answer by Jamles Hez Thursday, April 30, 2015 12:02 AM
Wednesday, April 22, 2015 2:03 PM
All replies
-
Hi,
Android and Windows Phone are different framework(classes ) use for same task.Plz For more details see following link to bluetooth ,
https://msdn.microsoft.com/en-us/library/windows/apps/jj207007(v=vs.105).aspx
Don't forget to mark the right answer and vote up if helps you.
Wednesday, April 8, 2015 12:31 PM -
Hi Siddhanath
Thanks for the reply. I am already familiar with the link you provided. I'm looking for coding assistance. I have a deep knowledge of vb6 etc, I have not really developed in .NET, hence my original question.
Regards
John.
www.islesystems.com | www.ursosbutton.com | www.atlas911.com
Wednesday, April 8, 2015 1:44 PM -
Hi Jmu5667,
Not sure if its too late to reply you. But looks like if you have the C# code, translate to VB.NET code is not a difficult thing.
You could find some convert engine for help if you search on the Internet, here I tried to convert some from the documentation Siddhanath provided. Hope helps.
Connect to a peer app
Private Sub AppToApp() ' PeerFinder.Start() is used to advertise our presence so that peers can find us. ' It must always be called before FindAllPeersAsync. PeerFinder.Start() Dim peers = Await PeerFinder.FindAllPeersAsync() If peers.Count = 0 Then Debug.WriteLine("Peer not found.") Else ' Select a peer. In this example, let's just pick the first peer. Dim selectedPeer As PeerInformation = peers(0) ' Attempt a connection Dim streamSocket = Await PeerFinder.ConnectAsync(selectedPeer) DoSomethingUseful(streamSocket) End If End Sub
Connect to a device
Private Sub AppToDevice() ' Configure PeerFinder to search for all paired devices. PeerFinder.AlternateIdentities("Bluetooth:Paired") = "" Dim pairedDevices = Await PeerFinder.FindAllPeersAsync() If pairedDevices.Count = 0 Then Debug.WriteLine("No paired devices were found.") Else ' Select a paired device. In this example, just pick the first one. Dim selectedDevice As PeerInformation = pairedDevices(0) ' Attempt a connection Dim socket As New StreamSocket() ' Make sure ID_CAP_NETWORKING is enabled in your WMAppManifest.xml, or the next ' line will throw an Access Denied exception. ' In this example, the second parameter of the call to ConnectAsync() is the RFCOMM port number, and can range ' in value from 1 to 30. Await socket.ConnectAsync(selectedDevice.HostName, "1") DoSomethingUseful(socket) End If End Sub
Listen for a connection request
' Page Constructor Public Sub New() InitializeComponent() AddHandler Loaded, AddressOf MainPage_Loaded End Sub Private Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) AddHandler PeerFinder.ConnectionRequested, AddressOf PeerFinder_ConnectionRequested End Sub Private Sub PeerFinder_ConnectionRequested(sender As Object, args As ConnectionRequestedEventArgs) If ShouldConnect() Then ' Go ahead and connect ConnectToPeer(args.PeerInformation) End If End Sub Private Sub ConnectToPeer(peer As PeerInformation) Dim socket As StreamSocket = Await PeerFinder.ConnectAsync(peer) DoSomethingUseful(socket) End Sub Private Function ShouldConnect() As Boolean ' Determine whether to accept this connection request and return Return True End Function
Detect the state of the Bluetooth radio on your phone
Private Sub FindPaired() ' Search for all paired devices PeerFinder.AlternateIdentities("Bluetooth:Paired") = "" Try ' Handle the result of the FindAllPeersAsync call Dim peers = Await PeerFinder.FindAllPeersAsync() Catch ex As Exception If CUInt(ex.HResult) = &H8007048fUI Then MessageBox.Show("Bluetooth is turned off") End If End Try End Sub
--JamesWe are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Proposed as answer by Jamles Hez Wednesday, April 22, 2015 2:03 PM
- Marked as answer by Jamles Hez Thursday, April 30, 2015 12:02 AM
Wednesday, April 22, 2015 2:03 PM -
Hi James
thanks for the efforts you have put in. I will try your suggestions today. I put the BT issue on hold for the last few days and focused my efforts on sockets steams, which I have succeed in doing so. I will keep you posted.
Just to be Clear this is windows phone app that connects to our device, www.ursosbutton.com.
Regards
John.
www.islesystems.com | www.ursosbutton.com | www.atlas911.com
- Edited by Jmu5667 Thursday, April 30, 2015 11:24 AM
Thursday, April 30, 2015 11:21 AM -
Hi James
thank you so much, I have now managed to list all paired devices. Our device uses Serial Port Profile, So I would like to use SPP and listen for the connection from the device, received data (using the stream the streamsocket), and do my stuff based on the data I received.
Curently, all of this is happening on pages, the finished product will have GPS/BT running as background services.
I hope it wont be too difficult to achieve this. Can you help me with the SPP bit please.
Regards
John.
www.islesystems.com | www.ursosbutton.com | www.atlas911.com
Thursday, April 30, 2015 12:05 PM