Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.andpair;
- import java.util.HashMap;
- import java.util.Iterator;
- import android.hardware.usb.UsbConstants;
- import android.hardware.usb.UsbDevice;
- import android.hardware.usb.UsbDeviceConnection;
- import android.hardware.usb.UsbEndpoint;
- import android.hardware.usb.UsbInterface;
- import android.hardware.usb.UsbManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.bluetooth.BluetoothAdapter;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- public class MainActivity extends Activity implements OnClickListener {
- private static final String ACTION_USB_PERMISSION =
- "com.xerpi.andpair.USB_PERMISSION";
- private final int VENDOR_ID = 0x054C; //Sony Corp.
- private final int PRODUCT_ID = 0x0268; //Sixaxis and DS3
- Button scanButton, setMACbutton, getMACbutton, getDeviceMACbutton;
- TextView textViewGetMAC;
- EditText editTextSetMAC;
- /** AndPair **/
- UsbManager usbManager;
- UsbDevice usbDevice;
- HashMap<String, UsbDevice> deviceList;
- UsbInterface usbInterface;
- UsbEndpoint endpoint;
- UsbDeviceConnection deviceConnection;
- boolean isConnected = false;
- Object mPermissionIntent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- scanButton = (Button)findViewById(R.id.buttonScan);
- setMACbutton = (Button)findViewById(R.id.buttonSetMAC);
- getMACbutton = (Button)findViewById(R.id.buttonGetMAC);
- getDeviceMACbutton = (Button)findViewById(R.id.buttonGetDeviceMac);
- getDeviceMACbutton.setOnClickListener(this);
- scanButton.setOnClickListener(this);
- setMACbutton.setOnClickListener(this);
- getMACbutton.setOnClickListener(this);
- textViewGetMAC = (TextView)findViewById(R.id.textViewGetMAC);
- editTextSetMAC = (EditText)findViewById(R.id.editTextSetMac);
- usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
- mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
- IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
- registerReceiver(mUsbReceiver, filter);
- }
- public void setMAC()
- {
- if(!isConnected) return;
- }
- public void getMAC()
- {
- if(!isConnected) return;
- byte[] buf = new byte[8];
- deviceConnection.controlTransfer(
- UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_CLASS | UsbConstants.USB_INTERFACE_SUBCLASS_BOOT,
- 0x01,
- 0x03f5,
- 0,
- buf,
- buf.length,
- 5000);
- String MAC = MACstring(buf);
- dlog("MAC: "+MAC);
- textViewGetMAC.setText(MAC);
- }
- public void setMAC(String MAC)
- {
- if(!isConnected) return;
- byte[] buf = new byte[8];
- String[] MACsplitted = MAC.split(":");
- if(MACsplitted.length != 6) return;
- for(int i = 2; i <8; i++)
- {
- Integer hex = Integer.parseInt(MACsplitted[i-2], 16);
- buf[i] = hex.byteValue();
- }
- buf[0] = 0x1; buf[1] = 0x0;
- deviceConnection.controlTransfer(
- UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_CLASS | UsbConstants.USB_INTERFACE_SUBCLASS_BOOT,
- 0x09,
- 0x03f5,
- 0,
- buf,
- buf.length,
- 5000);
- }
- public String MACstring(byte[] macArray)
- {
- String MAC = new String();
- for(int i = 2; i < 8; i++)
- {
- MAC += String.format("%02X", (short)(macArray[i] & 0xFF));
- if(i < 7) MAC+=":";
- }
- return MAC;
- }
- public String getDeviceMAC()
- {
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- return mBluetoothAdapter.getAddress();
- }
- public void scan()
- {
- if(isConnected) return;
- dlog("Scan:");
- HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
- Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
- while(deviceIterator.hasNext())
- {
- UsbDevice device = deviceIterator.next();
- if(isDeviceVID_PID(device, VENDOR_ID, PRODUCT_ID))
- {
- dlog("SIXAXIS FOUND!");
- usbDevice = device;
- if(!usbManager.hasPermission(usbDevice))
- {
- dlog("Requesting permissions...");
- usbManager.requestPermission(usbDevice, (PendingIntent) mPermissionIntent);
- }
- else
- {
- dlog("Device already has permissions.");
- }
- }
- }
- }
- @Override
- public void onClick(View v)
- {
- switch(v.getId())
- {
- case R.id.buttonScan:
- scan();
- break;
- case R.id.buttonGetMAC:
- getMAC();
- break;
- case R.id.buttonSetMAC:
- setMAC(editTextSetMAC.getText().toString());
- break;
- case R.id.buttonGetDeviceMac:
- editTextSetMAC.setText(getDeviceMAC());
- break;
- default:
- break;
- }
- }
- public void dlog(String s)
- {
- Log.d("AndPair", s);
- }
- public boolean isDeviceVID_PID(UsbDevice device, int VID, int PID)
- {
- if(device.getVendorId() == VID && device.getProductId() == PID)
- return true;
- else
- return false;
- }
- private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
- {
- public void onReceive(Context context, Intent intent)
- {
- String action = intent.getAction();
- if (ACTION_USB_PERMISSION.equals(action))
- {
- synchronized (this)
- {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
- {
- if(device != null)
- {
- dlog("Got permissions!");
- usbInterface = device.getInterface(0);
- endpoint = usbInterface.getEndpoint(0);
- deviceConnection = usbManager.openDevice(device);
- if(deviceConnection.claimInterface(usbInterface, true))
- {
- dlog("Interface claimed!");
- isConnected = true;
- }
- else
- {
- dlog("Could not claim the interface.");
- }
- }
- }
- else
- {
- dlog("permission denied for device " + device);
- }
- }
- }
- else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
- {
- UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
- if (device != null)
- {
- deviceConnection.releaseInterface(usbInterface);
- deviceConnection.close();
- dlog("Device closed");
- }
- }
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement