Advertisement
xerpi

AndPair alpha 1 [Android]

Apr 6th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.97 KB | None | 0 0
  1. package com.xerpi.andpair;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5.  
  6.  
  7. import android.hardware.usb.UsbConstants;
  8. import android.hardware.usb.UsbDevice;
  9. import android.hardware.usb.UsbDeviceConnection;
  10. import android.hardware.usb.UsbEndpoint;
  11. import android.hardware.usb.UsbInterface;
  12. import android.hardware.usb.UsbManager;
  13. import android.os.Bundle;
  14. import android.util.Log;
  15. import android.view.View;
  16. import android.view.View.OnClickListener;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.TextView;
  20. import android.app.Activity;
  21. import android.app.PendingIntent;
  22. import android.bluetooth.BluetoothAdapter;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.content.IntentFilter;
  27.  
  28.  
  29. public class MainActivity extends Activity implements OnClickListener {
  30.    
  31.     private static final String ACTION_USB_PERMISSION =
  32.             "com.xerpi.andpair.USB_PERMISSION";
  33.     private final int VENDOR_ID   = 0x054C; //Sony Corp.
  34.     private final int PRODUCT_ID  = 0x0268; //Sixaxis and DS3
  35.    
  36.     Button scanButton, setMACbutton, getMACbutton, getDeviceMACbutton;
  37.     TextView textViewGetMAC;
  38.     EditText editTextSetMAC;
  39.     /** AndPair **/
  40.         UsbManager usbManager;
  41.         UsbDevice usbDevice;
  42.         HashMap<String, UsbDevice> deviceList;
  43.         UsbInterface usbInterface;
  44.         UsbEndpoint endpoint;
  45.         UsbDeviceConnection deviceConnection;
  46.         boolean isConnected = false;
  47.  
  48.         Object mPermissionIntent;
  49.    
  50.     @Override
  51.     protected void onCreate(Bundle savedInstanceState) {
  52.         super.onCreate(savedInstanceState);
  53.         setContentView(R.layout.activity_main);
  54.        
  55.         scanButton = (Button)findViewById(R.id.buttonScan);
  56.         setMACbutton = (Button)findViewById(R.id.buttonSetMAC);
  57.         getMACbutton = (Button)findViewById(R.id.buttonGetMAC);
  58.         getDeviceMACbutton = (Button)findViewById(R.id.buttonGetDeviceMac);
  59.         getDeviceMACbutton.setOnClickListener(this);
  60.         scanButton.setOnClickListener(this);
  61.         setMACbutton.setOnClickListener(this);
  62.         getMACbutton.setOnClickListener(this);
  63.        
  64.         textViewGetMAC = (TextView)findViewById(R.id.textViewGetMAC);
  65.         editTextSetMAC = (EditText)findViewById(R.id.editTextSetMac);
  66.        
  67.         usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
  68.  
  69.         mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
  70.         IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
  71.         registerReceiver(mUsbReceiver, filter);
  72.     }
  73.  
  74.     public void setMAC()
  75.     {
  76.         if(!isConnected) return;   
  77.     }
  78.    
  79.     public void getMAC()
  80.     {
  81.         if(!isConnected) return;
  82.         byte[] buf = new byte[8];
  83.         deviceConnection.controlTransfer(
  84.                 UsbConstants.USB_DIR_IN | UsbConstants.USB_TYPE_CLASS | UsbConstants.USB_INTERFACE_SUBCLASS_BOOT,
  85.                 0x01,
  86.                 0x03f5,
  87.                 0,
  88.                 buf,
  89.                 buf.length,
  90.                 5000);
  91.        
  92.         String MAC = MACstring(buf);
  93.         dlog("MAC: "+MAC);
  94.         textViewGetMAC.setText(MAC);
  95.        
  96.     }
  97.    
  98.     public void setMAC(String MAC)
  99.     {
  100.         if(!isConnected) return;
  101.         byte[] buf = new byte[8];
  102.         String[] MACsplitted = MAC.split(":");
  103.         if(MACsplitted.length != 6) return;
  104.        
  105.         for(int i = 2; i <8; i++)
  106.         {
  107.             Integer hex = Integer.parseInt(MACsplitted[i-2], 16);
  108.             buf[i] = hex.byteValue();
  109.         }
  110.         buf[0] = 0x1; buf[1] = 0x0;
  111.        
  112.         deviceConnection.controlTransfer(
  113.                 UsbConstants.USB_DIR_OUT | UsbConstants.USB_TYPE_CLASS | UsbConstants.USB_INTERFACE_SUBCLASS_BOOT,
  114.                 0x09,
  115.                 0x03f5,
  116.                 0,
  117.                 buf,
  118.                 buf.length,
  119.                 5000);
  120.        
  121.     }
  122.    
  123.     public String MACstring(byte[] macArray)
  124.     {
  125.         String MAC = new String();
  126.         for(int i = 2; i < 8; i++)
  127.         {
  128.             MAC += String.format("%02X", (short)(macArray[i] & 0xFF));
  129.             if(i < 7) MAC+=":";
  130.         }
  131.         return MAC;
  132.     }
  133.    
  134.     public String getDeviceMAC()
  135.     {
  136.         BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  137.         return mBluetoothAdapter.getAddress();
  138.     }
  139.    
  140.     public void scan()
  141.     {
  142.         if(isConnected) return;
  143.         dlog("Scan:");
  144.         HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();
  145.         Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
  146.         while(deviceIterator.hasNext())
  147.         {
  148.             UsbDevice device = deviceIterator.next();
  149.             if(isDeviceVID_PID(device, VENDOR_ID, PRODUCT_ID))
  150.             {
  151.                 dlog("SIXAXIS FOUND!");
  152.                 usbDevice = device;
  153.                 if(!usbManager.hasPermission(usbDevice))
  154.                 {
  155.                     dlog("Requesting permissions...");
  156.                     usbManager.requestPermission(usbDevice, (PendingIntent) mPermissionIntent);
  157.                 }
  158.                 else
  159.                 {
  160.                     dlog("Device already has permissions.");
  161.                 }
  162.             }
  163.         }
  164.        
  165.     }  
  166.    
  167.     @Override
  168.     public void onClick(View v)
  169.     {
  170.         switch(v.getId())
  171.         {
  172.         case R.id.buttonScan:
  173.             scan();
  174.             break;
  175.         case R.id.buttonGetMAC:
  176.             getMAC();
  177.             break;
  178.         case R.id.buttonSetMAC:
  179.             setMAC(editTextSetMAC.getText().toString());
  180.             break;
  181.         case R.id.buttonGetDeviceMac:
  182.             editTextSetMAC.setText(getDeviceMAC());
  183.             break;
  184.         default:
  185.             break;
  186.         }
  187.        
  188.     }  
  189.    
  190.     public void dlog(String s)
  191.     {
  192.         Log.d("AndPair", s);
  193.     }
  194.    
  195.     public boolean isDeviceVID_PID(UsbDevice device, int VID, int PID)
  196.     {
  197.         if(device.getVendorId() == VID && device.getProductId() == PID)
  198.             return true;
  199.         else
  200.             return false;
  201.     }
  202.    
  203.    
  204.     private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver()
  205.     {
  206.         public void onReceive(Context context, Intent intent)
  207.         {
  208.             String action = intent.getAction();
  209.             if (ACTION_USB_PERMISSION.equals(action))
  210.             {
  211.                 synchronized (this)
  212.                 {
  213.                     UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  214.  
  215.                     if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false))
  216.                     {
  217.                         if(device != null)
  218.                         {
  219.                             dlog("Got permissions!");
  220.                            
  221.                             usbInterface     = device.getInterface(0);
  222.                             endpoint         = usbInterface.getEndpoint(0);
  223.                             deviceConnection = usbManager.openDevice(device);
  224.                            
  225.                             if(deviceConnection.claimInterface(usbInterface, true))
  226.                             {
  227.                                 dlog("Interface claimed!");
  228.                                 isConnected = true;
  229.                             }
  230.                             else
  231.                             {
  232.                                 dlog("Could not claim the interface.");
  233.                             }
  234.                         }
  235.                     }
  236.                     else
  237.                     {
  238.                         dlog("permission denied for device " + device);
  239.                     }
  240.                 }
  241.             }
  242.             else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action))
  243.             {
  244.                 UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  245.                 if (device != null)
  246.                 {
  247.                     deviceConnection.releaseInterface(usbInterface);
  248.                     deviceConnection.close();
  249.                     dlog("Device closed");
  250.                 }
  251.             }
  252.         }
  253.     };
  254.    
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement