This is a tutorial for introducing how to read and write Near-Field-Communication tags using Android Project. First at all, need add the dependency repository and library into your gradle files:
Please replace latestVersion annotation with the latest maven version
Paste this in the activity if you're extending our class:
override fun onNewIntent(Intent intent){super.onNewIntent(intent)for(String message in getNfcMessages()){// message }}
Otherwise:
override fun onNewIntent(intent:Intent?){super.onNewIntent(intent)
val items:SparseArray<String>=NfcReadUtilityImpl().readFromTagWithSparseArray(intent)for(i in0until items.size()){// items.valueAt(i) }}
If you like the Map implementation more you might as well use:
override fun onNewIntent(intent:Intent?){super.onNewIntent(intent)for(message inNfcReadUtilityImpl().readFromTagWithMap(intent).values()){// message}}
Now you're able to read the NFC Tags as long as the library supports the data in it when held to the devices!
4. How to write into tags
Implement NFC Callback interfaces:
Implement your own business-case callbacks in order to write the byte buffer:
Let your activity implement TaskCallback and invoke the WriteCallbackNfc function:
mTaskCallback =object:TaskCallback{override fun onReturn(result:Boolean){}override fun onProgressUpdate(vararg values:Boolean?){}override fun onError(e:Exception){}}WriteCallbackNfc(mTaskCallback, mOpCallback).executeWriteOperation()
5. How to obtain tags' MAC
Use the following function as to get MAC of the tag
fun getMAC(tag:Tag):String{
val byteArrayToHexString =String.format("%0"+(tag.id.size *2).toString()+"X",BigInteger(1, tag.id))
val regex =Regex("(.{2})")return regex.replace(byteArrayToHexString,"$1:").dropLast(1)}
For instance:
val mac = getMAC(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG)asTag)// returns 0025:96FF:FE12:3456
6. License
by Romell DomÃnguez
Copyright 2016 Romell D.Z.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.