I'm using BLE.Plugin for scaning other device in Xamarin. There is no problem with scanning and the plugin finds some devices, but all the devices have the same mac-address. The found address doesn't match to the device addresses of the devices, that it should find. Below the code I'm using.
adapter.DeviceDiscovered += (s, a) =>
{
if (a.Device.NativeDevice is BluetoothDevice device)
{
if (addresses.Contains(device.Address))
{
...
}
}
};
await adapter.StartScanningForDevicesAsync();
Do you need to use the NativeDevice? The Id field on a.Device is the Unique Id for each bluetooth pheriperal that is found.
Seems you can't use the Mac address on iOS anyway and Id is compatible across platforms -> How to get Mac Address From CBPeripheral And CBCenter
Related
I would like to use a barcode scanner with Windows 10 (Build 15063) via the Windows.Devices.PointOfService namespace. The scanner is a Datalogic Quickscan QD2430 and I tried with all RS-232 and Keyboard mode.
I used the official sample application https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/BarcodeScanner with no luck. It can detect a device but it's definitely the in-built webcam (HP laptop).
I tried to modify the source, the DeviceHelpers's GetFirstDeviceAsync function https://github.com/Microsoft/Windows-universal-samples/blob/master/SharedContent/cs/DeviceHelpers.cs.
The DeviceInformation.FindAllAsync also returns only the camera's info as result.
string selector = BarcodeScanner.GetDeviceSelector(PosConnectionTypes.All);
DeviceInformation.FindAllAsync(selector);
It returns nothing.
DeviceInformation.FindAllAsync(DeviceClass.ImageScanner);
It returns every connected and I think the previously connected but currently offline devices too. I tried to filter the scanner by name. There was a lot filterd result too, but the convertAsync function returned null for all excepts one, it thrown an Exception "A device attached to the system is not functioning. (Exception from HRESULT: 0x8007001F)".
DeviceInformationCollection infos = await DeviceInformation.FindAllAsync(DeviceClass.All);
foreach(DeviceInformation info in infos)
{
if (info.Name.ToUpper().Contains("BARCODE"))
{
T scanner = await convertAsync(info.Id);
if (scanner != null)
{
return scanner;
}
}
}
Datalogic Quickscan QD2430 is not in the list of devices supported by Windows.Devices.PointOfService.
Ask Datalogic to provide a device driver that supports Windows.Devices.PointOfService, or change the scanner to the one described in the supported list.
Alternatively, create your own device driver according to the Point of Service (POS) of Windows Driver Kit.
(Cross posted from https://forums.xamarin.com/discussion/116840/android-bluetooth-device-list-friendly-names)
I have this C# method for Android to get a list of paired Bluetooth devices:
public List<string> PairedDevices()
{
var adapter = BluetoothAdapter.DefaultAdapter;
var devices = new List<string>();
foreach (var bd in adapter.BondedDevices)
{
devices.Add(bd.Name);
}
return devices;
}
Which works fine, except I need the list to show the same name as what shows on the list of devices that is displayed to users in the Android Bluetooth settings. If the user has renamed a device, the new name is not listed in the "Name" property. Name still contains the original name of the device before the user renamed it.
I'm new to Android development. I'm seeing some references to ExtraDevice and ExtraName but am not sure if this is what I need or how to obtain it. Help?
PS: am using a Xamarin Forms PCL solution, and the above method is part of a Dependency class borrowed from here: https://acaliaro.wordpress.com/2017/02/07/connect-a-barcode-reader-to-a-xamarin-forms-app-via-bluetooth/.
#Salman-Nasseem basically answered this in their comment. It turns out my question is misguided. If the user renamed a bluetooth device, the actual device name has not changed.
I'm trying to make a simple application that will show the nearby Bluetooth devices (this one works fine)
Also I'm trying to find if the specific Bluetooth device (I have its MAC & Its already paired in windows) is in range. It's an android phone and by default its set to Invisible to nearby devices, But that wouldn't be a problem since it's already paired in windows, right?
Am using this code:
BluetoothClient BTClient = new BluetoothClient();
BluetoothDeviceInfo[] BTDeviceInfo = BTClient.DiscoverDevices();
which is working find for finding "Visible Devices", It will also show me Paired device weather its In-Range or Not!
How i can check if that paired devices is in-range? Without going to setting and make it "Visible" will be much better
It might be too late, but you can use BTClient.DiscoverDevicesInRange();
and look for your device in that list.
Another method would be to try reading the services in the device using its bluetooth address, you will get an exception if it isn't in range.
private static Boolean IsInRange(BluetoothDeviceInfo device)
{
Guid fakeUuid = new Guid("{F13F471D-47CB-41d6-9609-BAD0690BF891}");
try
{
ServiceRecord[] records = device.GetServiceRecords(fakeUuid);
return true;
}
catch(SocketException ex)
{
if (ex.ErrorCode == 10040) return true;
return false;
}
}
I'm trying to open a USB device handle in MonoLibUsb (on Linux), but every time I open it I get IsInvalid == true on the device handle.
The USB device is definitely compatible with LibUsb as I've connected it to my Windows PC and can successfully use LibUsbDotNet to talk to it. If I try to use LibUsbDotNet in Mono the application hangs when trying to open it, so I figured LibUsbDotNet is for Windows and MonoLibUsb is for Mono (the name kind of gives it away). However, even MonoLibUsb fails to properly use the device.
So why is the device handle returned invalid?
Code
private void UsbInit() {
var sessionHandle = new MonoUsbSessionHandle();
var profileList = new MonoUsbProfileList();
profileList.Refresh(sessionHandle);
List<MonoUsbProfile> usbList = profileList.GetList().FindAll(MyVidPidPredicate);
foreach(MonoUsbProfile profile in usbList) {
var deviceHandle = profile.OpenDeviceHandle();
if (deviceHandle.IsInvalid) {
Console.WriteLine(string.Format("IsInvalid: {0} - {1}", MonoUsbSessionHandle.LastErrorCode, MonoUsbSessionHandle.LastErrorString));
}
}
}
private bool MyVidPidPredicate(MonoUsbProfile profile) {
if (profile.DeviceDescriptor.VendorID == 0xabcd && profile.DeviceDescriptor.ProductID == 0x1234)
return true;
return false;
}
Output
IsInvalid: Success -
This line in the documentation is very easy to overlook:
The user must have appropriate access permissions to the usb device before it can be used with linux.
If I'm starting the application as root (or through sudo) the device handle becomes valid.
I'm writing a Windows Phone 8.1 Application that discovers nearby Bluetooth Low Energy devices.
foreach (DeviceInformation device in devices)
{
BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
}
Everything works fine, but the bleDevice.BluetoothAddress property contains a ulong type, while I need a string type, formatted like a Mac Address.
Example:
bleDevice.BluetoothAddress: 254682828386071 (ulong)
Desired Mac Address: D1:B4:EC:14:29:A8 (string) (that's an example of how I need it, not the actual Mac Address of the device)
Is there a way to convert the long to a Mac Address? Or is there another way to directly discover the Mac Address without conversions? I know there's a tool named In The HAnd - 32feet that could help me, but as of now Windows Phone 8.1 is not supported.
There are numerous topics you can find through Google and here on StackOverflow. Anyway, here's one way to do it:
ulong input = 254682828386071;
var tempMac = input.ToString("X");
//tempMac is now 'E7A1F7842F17'
var regex = "(.{2})(.{2})(.{2})(.{2})(.{2})(.{2})";
var replace = "$1:$2:$3:$4:$5:$6";
var macAddress = Regex.Replace(tempMac, regex, replace);
//macAddress is now 'E7:A1:F7:84:2F:17'