Read data from HID device in iOS - ios

How to read data from HID device onto my iOS app in Objective C? I am trying to scan a barcode using external scanner and retrieve the data.

Have you checked this out?
https://developer.apple.com/library/content/samplecode/EADemo/Introduction/Intro.html
Also, having worked on this problem previously, I found non-MFI HID devices (like all the cheap BT scanners on eBay) function only as an external keyboard on iOS, unless you go the guidelines-breaking route #vitormm presented. To use an external keyboard for scanning in a guideline-compliant way, you need a UITextField or UITextView to be firstResponder when the user hits the scan button to capture the scan, or the scan will go into the bit bucket. From there, you'll need to use the textDelegate protocol and a timer to discern the end of the scan, or program the scanner to send a terminator character like a Carriage Return in between scans, and scan the input stream for the terminator.
Additionally, in my survey from about a year ago, I found most manufacturers of MFI barcode scanners provide a library to interface to their scanners. These function on a delegate callback or NSNotification type of architecture, which is what you really need for any sort of sophisticated mobile scanning application.

Related

How do I pair and/or bond to BLE on iOS using Swift code and an HM-10 so data sent is encrypted?

Background
I've built a simple circuit which includes an Arduino Nano and a HM-10 BLE serial module.
You can see that circuit at the following Electrical Engineering SE question: https://electronics.stackexchange.com/questions/412525/when-i-write-data-to-a-ble-characteristic-ffe1-to-send-data-over-bluetooth-is
I also have an Xcode project written in Swift that successfully sends bytes to the FFE1 characteristic.
It all works great and I can see the data coming from my iPad via Arduino Serial monitor.
However, from the research I did (see previous link) I believe I've determined that the data I send using Core Bluetooth writeValue() is not encrypted.
What I'm Trying To Do
Now that I've successfully sent and received bytes I would like to cause my iPad to bond with the BLE device (HM-10) so I can insure the data I send is encrypted via the provided BLE channel.
What I've Tried
Read through the Core Bluetooth docs at Apple, but I cannot find how to pair or bond to the device so that the data sent over the channel will then be encrypted.
Read every SO question I could find, but can't find any iOS code that
shows me how to bond and send data securely.
I did find some very basic info that to get the devices to bond I
need to try to write to a characteristic of the BLE device that has
the property CBCharacteristicPropertyNotifyEncryptionRequired but the
documentation on that at Apple is a ridiculous one-liner.
I wrote code that iterated through the characteristics of the HM-10
and read the properties and I didn't seem to find that property. But
the docs on the HM-10 seem to indicate that it does support a
authentication and encryption and I know that is a part of BLE.
Questions
Can you point me to Swift code that gives me an example of what I will do to bond and send encrypted data? Is there some property I need to set on CBPeriperhal or some param I need to send into writeValue()?
Am I misunderstanding? Does the HM-10 not support sending encrypted
data?
After posting my question I searched for pairing / bonding again.
I was taken to a link to a datasheet for the HM-10 which I've read numerous times. This time however, I searched for the word "bond" inside the document.
When I did that I read the following chart in the document:
This started me thinking and guessing.
I believe the issue is that the HM-10 is sent out as TYPE 0 so that hobbyist users can get it set up without pairing.
This is quite different from the HC-05 modules (classic Bluetooth) that I've used numerous times in other projects and which require pairing.
Possible Solution
I believe all I need to do is set the AT_TYPE to 2 and then my iOS (iPad) device will then allow me to pair using the PIN.
I'm guessing that it will then force every user to use the PIN to pair their device and then data will only be able to be written (using writeValue) via the secure BLE channel.
Extra Information
This is interesting to me also because when I use the default Bluetooth iOS scanning the OS never sees the HM-10-- the name doesn't appear under Bluetooth section. I am guessing that is because the HM-10 is not broadcasting that it can pair via pin and iOS ignores it.
That was always interesting to me because even though iOS couldn't see the HM-10, I could connect to it and send data to it via my Swift code. Wow, this may all be coming together finally.
I will test this tonight and post back to confirm if setting the TYPE solves it.
One More Thing
None of the extensive reading that I've done on the HM-10 (see Martyn Curry's fantastic, detailed articles) have ever mentioned this option and I've not seen it discussed anywhere.
I also see that some sites indicate that AT+TYPE is for setting whether the device is a Master/Slave (Central/Periperhal) which I don't believe is correct documentation for that item. This has all made it additionally difficult to determine how to get this working.
Update Note
I was reading yet another datasheet for HM-10 and this one shows that the default setting for AT+TYPE is in fact 0 (PIN code not needed), shown at the bottom of the chart. Not sure why the other datasheet didn't have this same info since it looks like the same chart.
Update 2 - This is the answer. I powered up the HM-10 and ran the Arduino code that allows me to send AT commands and sent AT+TYPE3<ENTER> and then sent AT+RESET to insure the HM-10 would restart itself.
After that, there is nothing else you need to do. I used my iOS app running on my iPad to send data to the FFE1 characteristic. When I attempted to do so, iOS popped up the default BLE pair/bond dialog and asked or the PIN (default is 000000 on these HM-10 components).
Note One thing I found was that one of my HM-10 is faulty. It allows data to be written even if I cancel the pairing or fail pairing (by sending incorrect PIN). That manufacturer's component is no longer available so I'm assuming that it is in error. The other HM-10 I have works exactly as expected. If you don't pair or fail to pair then the app fails to write the data. I will mark this as answer as soon as I can.

Catch scan of external barcode scanner while app not running

Currently I'm writing an application which will work with external barcode scanners over Bluethooth. I'm currently trying several different scanners and it seems they all simulate keyboard input if they scan a code. But now I've found a Socket Mobile scanner, which is certificated by apple and delivers a SDK.
What I need is a way to scan a code while the iPhone is in my pocket without the display unlocked (maybe in background if thats possible?).
So I want to know if it's even possible what I'm trying and if it is, can I achieve this with a Keyboard simulation or is the SDK thing a better way?
I would like to post some code at this point, but I've absolutely no idea where to start and I try to find something on google for 2 days now.
Thanks for help.

Communicating using Core Bluetooth

I am new to Objective c and I want to send simple strings from an iPhone to an arduino an vice versa with Bluetooth. I have read apples information about Core Bluetooth, but I am having trouble understanding it. As I said, all I need the iPhone to do is: 1) connect to the BLE device of the arduino. 2) Send instructions (in the form of strings) 3)get a response from the arduino when the instructions have been carried out, so that the next set of instructions can be sent. I would be very happy if somebody is able to help me with this project
Thanks
You should follow the wwdc presentation on core bluetooth.
You should probably start here
I suggest operate your device with YMSCoreBluetooth.
The basic step to operation bluetooth device is:
Search all nearby device by blue tooth scan, you will find all nearby device.
Stop your scan. Connect one of the devices found in response.
Discover all services on the device.
Discover all character in service found by service discover action.
Write your content into character found in 4.
You can read character value from device.
YMSCoreBluetooth let you write your logic in one code block instead of separate delegate.

Bluetooth Smart: is it sensible to choose a device based on its peripheral name

I have an application (Basically an Arduino) that I connect to from iOS (and might use Android in the future as well) using BLE. I reprogram the BLE hosts to a specific value so that the when I ask the peripherals name after it gets discovered in iOS can be accepted or rejected.
I'm however not sure that this is a good way to do. The name has a code that users can easily change to an other (always valid) code. That way if two modules are up and running at the same time, users will be sure that only the module they are linked to is the one they want to be linked to. This is a bit the same like connecting a mouse / keyboard of course using 'normal' bluetooth.

Using barcode scanner (like Grabba) from with-in my iOS application

My objective is simple: Read the data from a barcode scanner.
I know that there's an option for scanning the barcode using camera, but in my experience the results are not very accurate in less than ideal situation i.e. bad lighting. So, I'm exploring if an external device can be easily connected with the iPhone/iPad and which can provide the barcode data just like an external keyboard.
So, can I read the barcode, which is scanned using an external device? If so, can I do it without writing any code, or will I have to specifically add some kind of support in my application?
Answer
Any iOS compatible Bluetooth scanner that supports HID mode. For instance, any Socket CHS. Once connected, it will behave exactly as you said "just like an external keyboard".
HID vs SDK
HID: Using any scanner as a keyboard, you are limited to inputting scanned data into open input fields where the user can modify the scanned data and there are limited options for post-processing and validation.
Because the scanner appears to iOS as a keyboard, iOS hides the onscreen keyboard - which makes sense... if the scanner were an actual keyboard. Some scanners (incl. Socket CHS 7Ci & 7Xi) provide a mechanism to force the keyboard (On our CHS double-click the power button) or the app can force the keyboard (see: related stackoverflow questions)
SDK: Developing an app using the SDK provides a more robust setup and handles more complex use cases: Scan into the application even if an input field is not active, enforce a particular scan order (Scan a UPC followed by a VIN number, not vice versa), or parse the scanned data (Breaking a timedate stamp into year, month, day, time).
Conclusion: Personally, I'd use HID mode as a quick and dirty way to get started, but would switch to the SDK for the final product. Obviously, it depends on how much control you need/want.
Disclaimer: I work for Socket Mobile
You can download the demo from the link
http://www.datecs.bg/en/products/Linea-Pro-iPhone-SDK/8/102
Just run LineaDemo after unzip it. It's scanning using Linea Pro Scanner[External device]

Resources