Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Commit

Permalink
Fixed error in writeBlock, improved example, improved method document…
Browse files Browse the repository at this point in the history
…ations and more

-  Renamed uuid field to uid and renamed getter. Old getter kept for backwards compatibility
-  AUTH_A and AUTH_B constants are now public
-  authenticateCard no longer requires uid parameter, old name kept for backwards compatibility
- readBlock now takes buffer (byte array) parameter. The value read is stored there and the method returns a boolean indicating if the read was successful or not. If used without the buffer parameter, the value array will be returned, with no way to check if the read was successful or not
- write renamed to writeBlock, old name kept for backwards compatibility
- Fixed bug in writeBlock that was causing all writes to fail
- New static function: getBlockAddress, given a sector and block, returns the block's absolute address
- Updated README.md
- Updated example app
- Bumped to version 1.2.0
- Fixes #2
  • Loading branch information
Galarzaa90 committed Jul 8, 2017
1 parent f65eba8 commit 85b5ab7
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 89 deletions.
1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,12 @@ The connections vary based on the [board](https://developer.android.com/things/h
**RST** pin is configured programatically.

## Installing
This library is available at Bintray. To install add this to your module's build.gradle
This library is available at jCenter. To install add this to your module's build.gradle
```groovy
repositories {
maven {
url 'http://dl.bintray.com/galarzaa90/maven/'
}
}
dependencies {
compile 'com.galarzaa.android-things:rc522:0.1.2'
compile 'com.galarzaa.android-things:rc522:0.2.0'
}
```

Expand Down Expand Up @@ -60,7 +55,7 @@ public class MainActivty extends AppCompatActivity{
if(success){
success = rc522.antiCollisionDetect();
if(success){
byte[] uuid = rc522.getUiid();
byte[] uuid = rc522.getUid();
/* Perform any operations here */
break;
}
Expand Down
4 changes: 2 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ ext.versions = [
minSdk: 24,
compileSdk: 25,
buildTools: '25.0.3',
publishVersion: '0.1.2',
publishVersionCode: 3,
publishVersion: '0.2.0',
publishVersionCode: 4,
gradleVersion: '2.3.3',

thingsLib: '0.2-devpreview'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.galarzaa.androidthings.Rc522;
import com.google.android.things.pio.Gpio;
Expand All @@ -17,13 +18,18 @@
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

private Rc522 mRrc522;
RfidTask mRfidTask;
private TextView mTag;
private TextView mResultsDescription;
private Button button;

private SpiDevice spiDevice;
private Gpio gpioReset;

private static final String SPI_PORT = "SPI0.0";
private static final String PIN_RESET = "BCM25";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -40,20 +46,34 @@ public void onClick(View v) {
((Button)v).setText(R.string.reading);
}
});

PeripheralManagerService pioService = new PeripheralManagerService();
try {
SpiDevice spiDevice = pioService.openSpiDevice("SPI0.0");
Gpio resetPin = pioService.openGpio("BCM25");
mRrc522 = new Rc522(spiDevice, resetPin);
spiDevice = pioService.openSpiDevice(SPI_PORT);
gpioReset = pioService.openGpio(PIN_RESET);
mRrc522 = new Rc522(spiDevice, gpioReset);
} catch (IOException e) {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}


@Override
protected void onDestroy() {
super.onDestroy();
try{
if(spiDevice != null){
spiDevice.close();
}
if(gpioReset != null){
gpioReset.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

private class RfidTask extends AsyncTask<Object, Object, byte[]> {
private static final String TAG = "RfidTask";
private Rc522 rc522;

RfidTask(Rc522 rc522){
Expand All @@ -68,19 +88,47 @@ protected void onPreExecute() {
@Override
protected byte[] doInBackground(Object... params) {
while(true){
boolean success = rc522.request();
if(success){
success = rc522.antiCollisionDetect();
if(success){
return rc522.getUuid();
}
}
try {
Thread.sleep(100);
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
//Check if a RFID tag has been found
if(!rc522.request()){
continue;
}
//Check for collision errors
if(!rc522.antiCollisionDetect()){
continue;
}
byte[] uuid = rc522.getUid();
rc522.selectTag(uuid);
//We're trying to read block 1 in sector 2
byte block = Rc522.getBlockAddress(2,1);
//Mifare cards default key, this won't work if the key for the tag has been previously changed
byte[] key = {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
//Data that will be written to block 1, sector 2
byte[] newData = {0x0F,0x0E,0x0D,0x0C,0x0B,0x0A,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01};
boolean authResult = rc522.authenticateCard(Rc522.AUTH_A, block, key);
if(authResult){
if(rc522.writeBlock(block,newData)) {
Log.i(TAG, "Data written successfully.");
}else{
Log.w(TAG, "Could not write to tag");
}
//Byte buffer to hold data
byte[] buff = new byte[16];
if(rc522.readBlock(block,buff)) {
Log.i(TAG, "Data read: " + Arrays.toString(buff));
}else{
Log.w(TAG, "Could not read data from tag");
}
rc522.stopCrypto();
}else{
Log.e(TAG,"Could not authenticate tag.");
}
return uuid;
}
}

Expand Down
Loading

0 comments on commit 85b5ab7

Please sign in to comment.